Friday, November 21, 2008

How to connect MOSS(Sharepoint Server 2007) using c#.Net

Add Microsoft.Sharepoint.dll as a reference in your Project.
Use the following namespace for your Project to utilise the Sharepoint classes, properties and methods.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

Once you add the reference and namespace properly, intellisense will show you all the classes and methods properly.

mySite = new SPSite("http://Test-Sample:123/"); //You have to specify your site here.
SPWeb myweb = mySite.AllWebs["IT"]; //You have to speify the subsite here.
myweb.AllowUnsafeUpdates = true; //It will allow you to update listitem or List or anything from your code.
myweb.Update(); //It will update the web.

This code will be hardcoded. If you don't want to hardcode you can use the following code snippet

string Subsite = Request.QueryString["Subsite"]
mySite = new SPSite(this.Page.Request.Url.ToString());
SPWeb myweb = mySite.AllWebs[Subsite];
myweb.AllowUnsafeUpdates = true;

The above example is a good practice to implement it to your Project. In this project instead of subsite hardcoding, i used querystring to fetch the subsite from other forms. Based on the subsite value it will connect to your subsite.

No comments: