Friday, October 28, 2011

Create Custom Site Collection Web Service

Some time ago i needed to access to "Site Collection" information by Web Services. This type of Service is not able by SharePoint, not because is hard, but maybe for Security and Performance issues, 
I was making some investigation about the content of different SharePoint Site Collections and didn't have access to Central Administration for the API for that i try to use this Web Service to return some information that, since i am not able to use Out of the Box Web Services to response this requirement...

PS: Be careful with SharePoint Service Pack and CU and Security update that can change the API permission and block access to Site Collection content. Use by your own risk.

Validate if the user you will use to call all Site Collection have access to you Web Application with Read Permission,
For this, you can access you Central Administration
Central Administration > Application Management > Manage Web Applications > Select Web Application > Click on Ribbon "User Policy"

Validate if the account you will use have Full read Permission to access the Site Collection Content.

Create the Web Service

You can create your own Custom Web Services following  the MSDN Article:
Walkthrough: Creating a Custom ASP.NET Web Service SharePoint 2010

For this example i create a VS2010 Studio Project and add a Custom Web Service to return some Site collection properties, this Web Service will be mapped to be deployed in the Folder "ISAPI" you can find in the SharePoint installation folder "14"

Here example code to call the Site collections:
                SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteCollections = webApp.Sites;
                foreach (SPSite siteCollection in siteCollections)
                         //Return Some Site Collection Properties...
                        if (siteCollection != null)

To deploy this solution in your SharePoint Farm you can use the PowerShell commands  in "SharePoint 2010 Management Shell"
  • Add-SPSolution -LiteralPath <SolutionPath>/WebServiceSiteCollections.wsp
  • Install-SPSolution -Identity WebServiceSiteCollections.wsp -GACDeployment

Test the Web Service with SharePoint Designer

To validate if the Custom Web Service is accessing without problem you can make the call  with "http:/[Site]/_vti_bin/CustomSiteCollection.asmx"

One simple way to test this Custom Web Service, can be using SharePoint Designer 2010 and add a Data View Web Part to a SharePoint Page.

The first task is create a Data Source to our custom Web Service and select the Method List Site Collections.
This Data Source will be stored in the Data Sources List on the Left.

After you create your Data Source you can use in your SharePoint Page to display the Data associated, for this you can Edit a aspx page example:"Home.aspx" access to a Web Part Zone and select on Ribbon "Insert > DataVew > CustomSiteCollection".

A new View call "Data Source Details" will appear in the right with the SOAP Field of the Web Service, you can select  the field to Display and create a DataView of the Content.

After you change the Layout of your xslt in the Data View and save the page, preview you SharePoint Page and view you content displayed.

 If you are having issues accessing validate the permission requisites

Custom Site Collection Web Service Solution Link,

Hope you like...



No comments: