Here I am going to show you that the actions which we perform normally with UI, those also can be done programmatically, the same thing I am going to show in this post, this post is mainly targeted for beginners those who are new to Sharepoint object model, a quick watch on programs to create sub sites, lists, showing web apps etc.
Creating Sub Site:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); SPWebCollection myWebCol = myWeb.Webs; SPWeb mynewweb = myWebCol.Add("Web url", "Web Title", "Web Description", 1033, "STS#0", false, false); Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Creating List:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); myWeb.Lists.Add("My New List", "My new list description", myWeb.ListTemplates["Custom List"]); SPList newList = myWeb.Lists["My New List"]; newList.OnQuickLaunch = true; newList.Update(); Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all top-level sites in a farm
static void Main() { foreach (SPWebApplication myWebApp in SPWebService.ContentService.WebApplications) { foreach (SPSite mySiteCol in myWebApp.Sites) { try { Console.WriteLine(mySiteCol.Url); } catch (Exception e) { Console.WriteLine(e); } } } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all site collection in web application:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWebApplication myWebApp = mySite.WebApplication; SPSiteCollection mySiteCol = myWebApp.Sites; foreach (SPSite SingleSite in mySiteCol) { Console.WriteLine(SingleSite.Url.ToString()); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all subsites in site collection:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); foreach (SPWeb myWeb in mySite.AllWebs) { Console.WriteLine(myWeb.Url.ToString()); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Roles in a site:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); foreach (SPRoleDefinition myRoleDef in myWeb.RoleDefinitions) { Console.WriteLine(myRoleDef.Name); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Alerts in a site:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); foreach (SPAlert myAlerts in myWeb.Alerts) { Console.WriteLine(myAlerts.Title); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Lists in a site:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); foreach (SPList myList in myWeb.Lists) { Console.WriteLine(myList.Title.ToString()); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all List Templates in a site:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); foreach (SPListTemplate myListTemplate in myWeb.ListTemplates) { Console.WriteLine(myListTemplate.Name); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Fields in a List:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); SPList myList = myWeb.Lists["List Name"]; foreach (SPField myField in myList.Fields) { Console.WriteLine(myField.InternalName); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Items in a List column:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); SPList myList = myWeb.Lists["List Name"]; SPQuery myQuery = new SPQuery(); myQuery.Query = "";//Your Query SPListItemCollection myItemCol = myList.GetItems(myQuery); foreach (SPListItem myListItem in myItemCol) { Console.WriteLine(myListItem["Column Name"].ToString()); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Delete all Items from a list:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); SPList myList=myWeb.Lists["List Name"]; myWeb.AllowUnsafeUpdates = true; int count = 1; for (int i = 0; i < myList.ItemCount; i++) { SPListItem myListitem = myList.Items[0]; myListitem.Delete(); Console.WriteLine(count + " item(s) deleted"); count++; } myWeb.AllowUnsafeUpdates = false; Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Groups in a site:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); foreach (SPGroup myGroup in myWeb.Groups) { Console.WriteLine(myGroup.Name); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Users in a group:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); SPGroup myGroup = myWeb.Groups["Group Name"]; foreach (SPUser myUser in myGroup.Users) { Console.WriteLine(myUser.Name); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Show all Users in a group from UserCollection:
static void Main(string[] args) { SPSite mySite = new SPSite("http://servername:port"); SPWeb myWeb = mySite.OpenWeb(); SPUserCollection myUserCollection = myWeb.Groups["Group Name"].Users; foreach (SPUser myUser in myUserCollection) { Console.WriteLine(myUser.LoginName); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }
Hi Vijay,
I am facing small problem with sp object model programming. I wrote an stand alone windows based application in my local system to get all subsites in my site collection. Could please tell me how can I pass user credentials to share point object model programming ?
@ Rama Kishore: Using web services you can pass the network credentials and get all subsites in a site collection, please look in to the similar post http://www.fivenumber.com/a-quick-look-on-wss-out-of-box-web-services-part-2/
The post shows all the subsites in a site collection using the system default credentails, if you want to pass your own specified credentails you can comment the line allWebs.Credentials = System.Net.CredentialCache.DefaultCredentials; and then un-comment the below lines System.Net.NetworkCredential mycredentials = new System.Net.NetworkCredential(“g.vijaikumar”, “mypassword”, “fivenumber”);
allWebs.Credentials = mycredentials;
Thanks vijay
Hi Vijay,
I need small customization in this requirement, I want display my sub sites in tree view format ( Node 1 root sub site ->sub site -> sub site, Node 2 root sub site ->sub site -> sub site).Please help me out on this.
Hi Vijay,
Can you please tell me how to create a sub site with unique permissions?
While creating a sub-site programmatically we need to break the role inheritance, then have to 3 custom site groups for owners, members and visitors, then after we have to set the newly created groups as associated groups for the new web.
Then we have to add the users with in the groups, also we need to set the role definitions such as full control, contributor, visitor for the users. I will posting a new article on how to create a sub-site with unique permissions for sure in future.
Please do let me know in case if you need more information, thanks for looking into this.