Tuesday, May 27, 2008

Calling MOSS web services through code

Hi All,

There are many way to code. One way is to use Sharepoint object model, Other way is to use web services to communicate with server and code it.

Here is a way how we can use web services to accomplish our tasks.

Before we start looking in this, let me give you highlights of where these web services are located and later we will see that how these can be used.

All Web services are inside "_vti_bin"

WSS Web Services Web Reference

Administration Service       http:///_vti_adm/admin.asmx
Alerts Service       http:///_vti_bin/alerts.asmx
Document Workspace Service       http:///_vti_bin/dws.asmx
Forms Service       http:///_vti_bin/forms.asmx
Imaging Service       http:///_vti_bin/imaging.asmx
List Data Retrieval Service       http:// /_vti_bin/dspsts.asmx
Lists Service       http:///_ vti_bin/lists.asmx
Meetings Service       http:/// _vti_bin/meetings.asmx
Permissions Service       http:// /_vti_bin/permissions.asmx
Site Data Service       http:///_vti_bin/sitedata.asmx
Site Service       http:///_vti_bin/sites.asmx
Users and Groups Service       http:///_vti_bin/usergroup.asmx
Versions Service       http:///_vti_bin/versions.asmx
Views Service       http:///_vti_bin/views.asmx
Web Part Pages Service       http:// /_vti_bin/webpartpages.asmx
Webs Service       http:///_vti_bin/webs.asmx

The SharePoint web services only accept calls from existing SharePoint users and do also enforce access security. Import the System.Net namespace into your project and then use the NetworkCredential class to set the user credential to use for the web service call. Here is a code snippet:

public static XmlNode VersionsGetVersions(string SharePointHost, string UserName, string Password, string Domain, string FileName)
{
// proxy object to call the Versions web service
Versions.Versions VersionsService = new Versions.Versions();
// the user credentials to use
VersionsService.Credentials = new NetworkCredential(UserName, Password, Domain);
VersionsService.Url = SharePointHost + "_vti_bin/Versions.asmx";
// gets the file versions
XmlNode Result = VersionsService.GetVersions(FileName);
// dispose the web service object
VersionsService.Dispose();
return Result;
}

If the user does not exist in SharePoint or does not have the permission to perform the operation then a WebException is thrown by SharePoint. The returned HTTP status is 401, which means unauthorized request

That's it. your job is done.

5 comments:

Unknown said...

"If the user does not exist in SharePoint or does not have the permission to perform the operation then a WebException is thrown by SharePoint. The returned HTTP status is 401, which means unauthorized request"

Does the user account calling the webservice need to be added to a specific security group for sharepoint to allow access? I have added an account at site level to have full control but i still get this error!

Anonymous said...

will the web part using this web service will run for any server having proper credential as you are adding the web reference for a particular server 's web service (http:///_vti_bin/Version.asmx)?

SharePoint Kings said...

if your URL is accessible form the server you are calling and if you have a valid credential then you can access it.

Unknown said...

hi im trying to connect to the Sharepoint link using webservice but it throws error as unable to connect as there could be firewal blocking or it cannot conect to remote server

SharePoint Kings said...

sashi,

if your URL is accessible form the server you are calling and if you have a valid credential then you can access it.

first try to open web service from browser if it allow then use your credential to access if from code.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com