26 January 2011

Access SharePoint from an application



SharePoint is a recognized technology standard for company infrastructure organizations. It has wide functionality, and allows storing different information in one single place. Many a times, we may want to upload documents in a SharePoint server and place these documents in a document library. Sometimes this information needs to be accessed from an application. We also had a requirement where we need to talk to SharePoint from our .NET application. This is not something new, but I would like to share my experience when there was a requirement to communicate SharePoint server from web application.
There are different ways where you can operate with share point.
·       Reference and use the classes the Microsoft.Sharepoint library (SPS)
·       Make a web reference to the SharePoint services. (WSS)
·       Using windows explorer. ( Classic way)
Microsoft.SharePoint library is the easier coding method of the two, but it requires that you run the program on the same server as SharePoint. In real time this is not the case, Production server and share pint server will be always different. In this case we need to add the reference of the SharePoint library and started accessing the functionalities. This is also called object model method.
Coming back to second method, add a reference to the SharePoint Lists service (/_vti_bin/Lists.asmx) and name it ‘ListsService’. The code was written against MOSS 2007. In SharePoint, the web services are located in http:///_vti_bin/lists.asmx. For example, if the SharePoint site that you are trying to access is http://Server name/SomeSite, the URL to the web service is http://Server name/Some Site/_vti_bin/lists.asmx · In the URL field, enter the URL to access the web services.
1.   Add a reference to the System.Net library. You will need this
library to perform authentication to the SharePoint site.
using System.Net;
2.  In the Main function, add a reference to your web reference
with the following statement:
SharePointSvce.Lists listService = new SharePointSvce.Lists();
This statement indicates that you will be using the Lists web service.
    3. In the next line,    
you will be setting up the authentication credentials to the   SharePoint site.
a. To use the logged in user’s credentials, add the following line:
listService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
        b. To use another set of credentials, add the following line:
listService.Credentials = new NetworkCredential(“userID”,
“password”, “domain”);
4.   Replace “userID” with the user ID to log in. 
Windows SharePoint Service has exposed 14 or more web services (please search for the names ) where applications can consume those and do all the operations where we can perform through SharePoint portal server. We have to create proxy as given above for each service that we are going to use from them.
The last method was to access share point server as windows explorer. This is the classic method and the server should be in same network to access SharePoint library folder. All the files which are uploaded like this can be accessed through SharePoint portal server also. We can follow this logic if we are in same network, if we have security issues to access web services in SharePoint server.