A quick look on Sharepoint object model programs
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();
}
Comments
Comment from Rama Kishore
Time April 19, 2010 at 7:42 am
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 ?
Comment from G Vijai Kumar
Time April 19, 2010 at 9:45 am
@ 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;
Comment from Rama Kishore
Time April 20, 2010 at 5:00 am
Thanks vijay
Comment from Rama Kishore
Time April 20, 2010 at 11:33 am
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.


Pingback from Microsoft Office Sharepoint Server » A quick look on Sharepoint object model programs – Part 2
Time March 29, 2010 at 12:04 pm
[...] In my previous post you can have a quick look on Sharepoint object model programs – Part 1 [...]