SharePoint Object Model 101
It is important to familiarize yourself with SharePoint Object Model as a SharePoint developer. This is a back to basics post about SharePoint Object model on the server side; in the next post I will introduce the SharePoint client side object model 101.
1. Retrieve all webs from site collections
using (SPSite site = new SPSite("http://YBBEST")) { foreach (SPWeb web in site.AllWebs) { Console.WriteLine("SPWeb title: "+web.Title); } }
2. Loop through all lists within the web
using (SPSite site = new SPSite("http://YBBEST")) { using (SPWeb web = site.OpenWeb()) { foreach (SPList spList in web.Lists) { Console.WriteLine(spList.Title); } } }
3. Different between AvailableFields and Fields.The Fields collection contains the all site columns in the current site while AvailableFields collection contains all site columns that can be used in the current site. (It includes site columns in the current site and its parent site).The Field in the AvailableFields collection is read-only and you cannot modify it while you can modify the Field in the Fields collection.
4. Get Item Field Value
using (SPSite site = new SPSite("http://YBBEST")) { using (SPWeb web = site.OpenWeb()) { SPList spList = web.Lists.TryGetList("Tasks"); if (spList != null) { Console.WriteLine("Success!"); Console.WriteLine(string.Format("Title : {0}", spList.Title)); Console.WriteLine(string.Format("Task Status : {0}", spList.Items[0]["Status"])); } else { Console.WriteLine("List does not exist !"); } } }
5.Using console application to call SharePoint web services.