Using SPSite and SPWeb Objects in SharePoint
SPSite is the
most fundamental object of the SharePoint Object Model,SPSite object can be
created using the overloaded constructors or it can be referenced by using
unique ID GUID, or with URL
Overloaded methods
SPSite(Guid id)
SPSite(string requestUrl)
SPSite(Guid id, SPUrlZone zone)
SPSite(Guid id, SPUserToken userToken)
SPSite(string requestUrl, SPUserToken userToken)
SPSite(Guid id,
SPUrlZone zone, SPUserToken userToken)
SPSite is the only through which we can access the SPWeb
because SPWeb does not have a constructor, There are way with which we
can get the instance of SPWeb is
through using “SPControl” and “SPContext”
Some of the important members of SPWeb are
1.
Users
2.
Title
3.
Update
4.
Delete
5.
ContentTypes
6.
AllUnsafeUpdates
7.
List
8.
ID
9.
GetSiteData
And so on.
Sample code shows how to access the SPWeb using SPSite and
display the title of the site collection and update the specific Subsite Tilte
namespace SampleDemo
{
[ToolboxItemAttribute(false)]
public class Sample : WebPart
{
protected override void CreateChildControls()
{
}
protected override void RenderContent(HtmlTextWriter writer)
{
SPSite mySiteCollection=null;
SPWeb mySPweb=null;
Try
{
//Get
the SPSite by passing the url
mySiteCollection = new SPSite("http://demoSite /")
Writer.Write(“I am in Site Collection”);
//print /display the current site collection title
Writer.Write ("My Current Site Exist here: ", mySiteCollection.Url);
mySPweb = mySiteCollection.OpenWeb("MySubSite")
//Update the subsite title
mySPweb.Title
= mySPweb.Title + " Hey I have changed the Title";
mySPweb.Update();
}
Catch(SPExecption
exception)
{
Throw
exception
}
Finally
{
mySiteCollection.Dispose();
mySPweb.Dispose();
}
}
}
}
No comments:
Post a Comment