SharePoint au Quotidien

 

Retour page Accueil
Remonter
WP2 Français
WP3 Anglais

 

 

 

 

 

 

 

 

Exemples de Web Services:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/SPSDKWelcome.asp

Description: Submitted By: Brien Posey


The Microsoft .NET framework provides many features and libraries which ease development time. In this article, I am going to discuss how to create a basic web part which uses Windows Management Instrumentation (WMI) queries to get some basic system information and display that information in a web part.

Web parts are based on the Web Control Library component, and inherit from the WebPart class in the SharePoint Library. To create the project, you will need Visual Studio .NET installed. Open Visual Studio .NET and create a new C# Web Control Library and name it WMIPart, as shown in the figure below.

http://msd2d.com/Content/Tip_viewitem.aspx?section=Sharepoint&category=Development&id=adcfe2a6-5ffc-490a-971a-af56738db274



You should now have a C# file which contains the base code for creating a Web Control Library. Since most of this code is not necessary for a WebPart, you can safely remove everything so that only the following remains:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WMIPart
{

}

The next thing we need to do is add the references for the SharePoint library and the WMI Management library. The SharePoint library contains everything necessary to create and work with web parts. The WMI management library contains code which allows us to run WMI queries from our web part.

To add these references, in Solution Explorer right-click References and choose Add Reference. Scroll down through the list until you find System.Management and click Select. That adds the Management library which contains all of the WMI commands. Adding the SharePoint library is a little trickier. The SharePoint library is located on your SharePoint server in the Program Files\Common Files\Microsoft Shared\web server extensions\60\ISAPI folder. If you are using Visual Studio .NET from the server you can browse to that location. If not, you will need to connect to a share on that server, such as the c$ administrative share, to locate that file. Click the Browse button and locate the SharePoint.dll file. As you can see from the screenshot below, I access the file through the \\exchange\e$ share. My server is named ‘exchange’ and Windows is installed on the E partition.



Click OK to compete adding these references.

Now that we have these reference libraries added to our project, we need to tell the code that it can use these libraries. To do so, at the beginning of the code, add these 2 lines:
using System.Management;
using Microsoft.SharePoint.WebPartPages;

While you are at it, you should also add:
using System.Runtime.InteropServices;the first line is used so you can set a GUID for this component which will uniquely identify it on the system. To define the GUID, in Visual Studio, go to Tools - Create GUID. Select 4. Registry format from the list and click the New GUID button. You should see something similar to the following:



Click the Copy button to copy the GUID to the clipboard. Now add the following line
[GuidAttribute("45E67728-56E1-4c65-8E50-9AD5309B44C5")]
after namespace WMIPart { but replace my GUID for the one you copied.

Now that most of the administrative framework is out of the way, we can start to code our actual web part. The first thing we need to do is create a class for our Web Part. Let’s call it ‘SysInfo’. This class will inherit from the WebPart base class. The WebPart base class contains all the code for rendering and displaying web parts on a SharePoint page. Directly after the GUID line, add:
public class SysInfo : WebPart
followed by a {.

When developing web parts, the 2 main functions you are going to use are CreateChildControls() and RenderWebPart(HtmlTextWriter output). The first function allows you to define the controls you would like to put in your web part. For example, in that section, you could have the code that adds text boxes and buttons to the web part. In our example we don’t have any controls, as the web part only renders html text. (In the code download, there will be a newer version of this project which has a “Refresh” button, so you can examine the download code for an example of how the CreateChildControls() function works).

The RenderWebPart(HtmlTextWrite output) function will be the main focus of our code. This function basically defines how a web part is rendered. Since all our code does is display text, all of our code can be placed in the RenderWebPart function. The following code searches the system for fixed disks and displays the total disk space and free disk space on all those disks.
protected override void RenderWebPart(HtmlTextWriter output)
{
ManagementObjectSearcher disks;
string text = "";
try
{
disks = new ManagementObjectSearcher("SELECT * FROM
Win32_LogicalDisk WHERE DriveType = 3")
foreach(ManagementObject o in disks.Get())
text += "Drive " + o["Name"].ToString() + " - Size " +
(Int64.Parse(o["Size"].ToString())/1048576)+ "MB,
Free Space " + Int64.Parse(o["FreeSpace"].ToString())/1048576) +"MB
";
disks.Dispose();
}
catch (Exception e)
{
text = e.ToString();
}

output.Write(text);
}
This code is fairly straightforward. We create a new WMI object searcher ‘disks’ which runs a WMI query for all the fixed disks on the system. Then we cycle through each disk to get their space information. That information is written to a string and then the output.Write(text) function actually writes the contents of the string to the web part in HTML. The code should now be all ready to compile. But wait, because this is a web part, the assembly must be signed with a public/private key pair and registered as a safe control on the web site.

To create a key pair, open a command windows and go to Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin. Run the command:
sn –k c:\WMIPart.snk
This will create the key pair and put them in the root of C:\. Now go back to Visual Studio. In the Solution Explorer open the file ‘AsemblyInfo.cs’ Replace the line:
[assembly: AssemblyKeyFile("")]
With
[assembly: AssemblyKeyFile("C:\\WMIPart.snk")]
(Note: the \\ is not a typo). Also while you are in there, replace the line
[assembly: AssemblyVersion("1.0.*")]
With
[assembly: AssemblyVersion("1.0.0.1")]
This gives the assembly a specific version number.

We are almost done now. It is time to build the project. Click Build
Build Solution. You now should have an assembly titled WMIPart.dll. (by default the file will be located in My Documents\Visual Studio Projects\WMIPart\bin\Debug). Now you need to copy that file to your SharePoint server to the Inetpub\wwwroot\bin directory. You will also want to copy it to Global Assembly Cache (GAC) on that server. (Microsoft claims that this is not necessary, but in my own testing I have found that this step is required). From the Assembly folder, right click on WMIPart and choose properties. You will notice there is a PublicKeyToken as shown below. You will want to copy that and save it for use later, because it will be necessary when registering the assembly as safe and for creating the web part definition.



Now that we have our web part defined in the GAC on the server, we need to declare the assembly as safe in our web.config file. In visual studio, go to File - Open - File. Browse to the Inetput\wwwroot folder on your server and open the file called web.config. In the section, add this line:

<SafeControl Assembly="WMIPart, Version=1.0.0.1, Culture=neutral,
PublicKeyToken=64bf77798fbca80f" Namespace="WMIPart" TypeName="*" Safe="True" />

but replace the PublicKeyToken with your own public key you copied in the previous step. Save the file.

All we have left to do is create the Web Part Definition file and import it. In Solution Explorer, right-click your project and choose Add - Add New Item. Select XML File and enter the name as WMIPart.dwp. Enter the following into that file:
<?xml version="1.0"?>

<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">

<Assembly>WMIPart, 1.0.0.1, Neutral, 64bf77798fbca80f</Assembly>

<TypeName>WMIPart.SysInfo</TypeName>

<Title>Disk Space</Title>

<Description>Web Part that displays free disk space</Description>

</WebPart>
Again remember to replace my Public key with your own. Save the file.

To import the file, open you SharePoint site. Click Modify my Page - Add Web Parts - Import. Click Browse and point to “WMIPart.dwp” created in the previous step. Click Upload and then Import.



There you have it. You have not only created you first web part, but you have leveraged the easy to use features of the .NET framework and WMI to display the free disk space on your server in a very small number of lines of code. There are two different sets of sample code. The first is sample code as done in this article. The second is a newer version of the assembly which not only displays free disk space, but memory usage and system uptime. It also includes a refresh button control in the web part.

Note: You may get the following warnings during the build:

The dependency 'Microsoft.SharePoint.Security' could not be found.
The dependency 'Microsoft.HtmlTrans.Interface' could not be found.
The dependency 'Microsoft.SharePoint.Dsp' could not be found.
The dependency 'Microsoft.SharePoint.Library' could not be found.

Do not worry, we do not use these namespaces and the project will compile anway.

http://msd2d.com/Content/Tip_viewitem.aspx?section=Sharepoint&category=Web%20Parts&id=1b53fa88-f94f-4e9a-9b0b-e96e7bf2a988

http://msd2d.com/Content/Tip_viewitem.aspx?section=Sharepoint&category=Web%20Parts&id=457766fb-cddc-48f7-93b5-0d592bac84f4

 

Google Web Service Client program

Submitted By: Smile Seo

http://msd2d.com/Content/FreeCode_viewitem.aspx?section=dotNet&id=3c76cc52-c57e-4f44-ae2d-44aaaef20dd7

 

.NET Webservice Studio

Submitted By: Microsoft Corporation
Posted On: 7/24/2002
Overall Rating: 0 (0 ratings, 0 comments) 

Description:
.NET Webservice Studio is a tool to invoke webmethods interactively. The user can provide a WSDL endpoint. On clicking button Get the tool fetches the WSDL, generates .NET proxy from the WSDL and displays the list of methods available. The user can choose any method and provide the required input parameters. On clicking Invoke the SOAP request is sent to the server and the response is parsed to display the return value.

View the
Readme for more details.

http://msd2d.com/Content/FreeCode_viewitem.aspx?section=dotNet&id=4724ab9c-0ce1-424b-983b-80a1894b1f59

http://www.newtelligence.com/wsextensions/

 

 

Retour page Accueil ] Remonter ] WP2 Français ] WP3 Anglais ]

Envoyez un courrier électronique à EROL GIRAUDY (attention nospam dans l'E-mail) pour toute question ou remarque concernant ce site Web et visitez la rubrique Condition Utilisation et CNIL. Copyright © 2002 EROL (les sigles et logos ci-après sont la propriété de : Microsoft, Supinfo, Adobe, Compaq, HP, Sybari, Veritas, Moreover, K-map, Vyapin, Plumtree, Ixos, TooStore, K-Map, eRoom, DocKIT,NQL, Only4gurus, Nsius, Sharepointexperts, Iora, Erol, KCura, FrontPages, Nsi, Frontlook, IBuySpyPortal, moreover, slipstick, networknowledge, clubsps.org )
Dernière modification : vendredi, 26. décembre 2003 11:27