I have created a Windows Forms Application to demonstrate the sample codes for Server Object Model using C# coding. Please include Microsoft.SharePoint and Microsoft.SharePoint.Administration DLLs in the project. These are available at “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI” folder. You can make changes to the form as per your requirement.
Sample Codes
Read Site Collection’s Title and Description.
You can access various properties of a Web. Below is a sample code:
public void getWebTitleDesc(string siteURL)
{
using(SPSite site=new SPSite(siteURL))
{
using(SPWeb web=site.OpenWeb())
{
textBox1.Text = web.Title;
textBox2.Text = web.Description;
}
}
}
Update Web Properties
web.AllowUnsafeUpdates is set to true or false. If any modification has to be done at site or web level, and the user do not have permission, then set the value of this property to true and make the changes after that, once the changes are done; change the value of this property to false otherwise all the following code will be executed irrespective of the user’s permission.
public void updateWebTitle(string siteURL)
{
string title=textBox1.Text;
using(SPSite site=new SPSite(siteURL))
{
using(SPWeb web=site.OpenWeb())
{
web.AllowUnsafeUpdates=true;
web.Title=title;
web.Update();
web.AllowUnsafeUpdates=false;
}
}
MessageBox.Show("Title is updated!");
getWebTitleDesc(siteURL);
}
Get All List Names
public void getAllLists(string siteURL)
{
using(SPSite site=new SPSite(siteURL))
{
using(SPWeb web=site.OpenWeb())
{
SPListCollection listColl = web.Lists;
foreach(SPList list in listColl)
{
listBox1.Items.Add(list.Title);
}
}
}
}
Create List
public void createList(string siteURL)
{
using(SPSite site=new SPSite(siteURL))
{
using(SPWeb web=site.OpenWeb())
{
web.Lists.Add("DemoList", "DemoListDesc", SPListTemplateType.GenericList);
web.Update();
MessageBox.Show("List added successfully!");
}
}
getAllLists(siteURL);
}
Add List Items
private void AddListItem()
{
string siteURL = txtSiteURL.Text;
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Products"];
SPListItem item = list.Items.Add();
item["Title"] = txtTitle.Text;
item["ProductID"] = txtProductID.Text;
item["ProductName"] = txtProductName.Text;
item["CostPerPiece"] = txtCost.Text;
item["Sale"] = txtSale.Text;
item.Update();
MessageBox.Show("List item updated successfully!");
}
}
Get All SubSites
public void getAllSubSite(string siteURL)
{
using(SPSite site=new SPSite(siteURL))
{
using(SPWeb web=site.OpenWeb())
{
SPWebCollection webColl = web.Webs;
MessageBox.Show("Total subsites in this Site Collection: " + webColl.Count);
foreach(SPWeb web1 in webColl)
{
listBox1.Items.Add(web1.Title);
}
}
}
Create Site Collection in current Web Application
public void createSiteCollection(string siteURL)
{
using(SPSite site=new SPSite(siteURL))
{
string topLevelSiteURL = site.WebApplication.Sites[0].Url;
site.WebApplication.Sites.Add(topLevelSiteURL+ "/sites/SOM2SiteColl", "SOM2SiteColl", "SOM2SiteCollDesc", 1033,
"STS#0", site.OpenWeb().CurrentUser.LoginName, site.OpenWeb().CurrentUser.Name, site.OpenWeb().CurrentUser.Email);
}
MessageBox.Show("Site Collection created successfully!");
}
Create SubSite
public void addSubSite(string siteURL)
{
using(SPSite site=new SPSite(siteURL))
{
using(SPWeb web=site.OpenWeb())
{
web.Webs.Add("MySubSite", "MySubSite", "SubSiteDesc", 1033, SPWebTemplate.WebTemplateSTS, false, false);
MessageBox.Show("SubSite Created successfully!");
}
}
}
Get Current User
public void getCurrentUser(string SiteURL)
{
using(SPSite site=new SPSite(SiteURL))
{
using(SPWeb web=site.OpenWeb())
{
SPUser user = web.CurrentUser;
textBox1.Text = user.Name;
}
}
}
Get All Web Application Names in current Farm
public void getAllWebApplications(string siteURL)
{
SPWebServiceCollection serColl = new SPWebServiceCollection(SPFarm.Local);
foreach(SPWebService service in serColl)
{
SPWebApplicationCollection webAppColl = service.WebApplications;
foreach(SPWebApplication webApp in webAppColl)
{
listBox1.Items.Add(webApp.DisplayName);
}
}
}