A quick look on Sharepoint object model programs – Part 3
I have got the chance to post useful Sharepoint object model programs (part 3) with bare minimum lines of code and really handy even.
Please follow the link for a quick look on Sharepoint object model programs Part 1 and Part 2
Creating a Folder in a Document Library:
static void Main(string[] args)
{
try
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
SPList MyLibrary = myWeb.Lists["Your Document Library Name....."];
SPFolder myFolder = MyLibrary.RootFolder;
SPFolder myChildFolder = myFolder.SubFolders.Add("Your Folder Name.....");
myFolder.Update();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Add fields to list:
static void Main(string[] args)
{
try
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists["Your List Name....."];
Guid listId = myList.ID;
myList.Fields.Add("Employee ID", SPFieldType.Number, true);
myList.Fields.Add("Employee Name", SPFieldType.Text, false);
myList.Fields.Add("Designation", SPFieldType.Text, false);
myList.Update();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Creating a new view in a list:
static void Main(string[] args)
{
try
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
SPList mylist = myWeb.Lists["Your List Name....."];
StringCollection myFields = new StringCollection();
myFields.Add("Employee Name");
myFields.Add("Designation");
SPView myView= mylist.Views.Add("Your View Name.....", myFields, null, 100, false, false, SPViewCollection.SPViewType.Html, false);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Creating a new Content Type:
static void Main(string[] args)
{
try
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
SPContentTypeCollection myContentTypeCollection = myWeb.ContentTypes;
SPContentType myContentType= new SPContentType(myContentTypeCollection[SPBuiltInContentTypeId.Item], myContentTypeCollection, "Content Type Name.....");
myContentTypeCollection.Add(myContentType);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Adding the existing Site Columns to existing Content Type:
static void Main(string[] args)
{
try
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
SPFieldCollection myFieldCollection = myWeb.Fields;
SPContentType myContentType=myWeb.ContentTypes["Content Type Name"];
myContentType.FieldLinks.Add(new SPFieldLink(myFieldCollection["Employee Name"]));
myContentType.FieldLinks.Add(new SPFieldLink(myFieldCollection["Designation"]));
myContentType.Update();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Save site as template:
static void Main(string[] args)
{
try
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
myWeb.SaveAsTemplate("FiveNumber.stp", "FiveNumber", "Its All About SharePoint", true);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Delete Recycle Bin Items:
static void Main(string[] args)
{
SPSite mySite = new SPSite("http://servername:port");
SPWeb myWeb = mySite.OpenWeb();
SPRecycleBinItemCollection myRecycleBinCollection = mySite.RecycleBin;
for (int i = 0; i < myRecycleBinCollection.Count; i++)
{
myRecycleBinCollection[i].Delete();
i--;
}
Console.WriteLine("Press any key to continue.....");
Console.ReadLine();
}
Comments
Comment from Sandeep
Time September 2, 2010 at 5:33 am
SPSite / SPWeb should be disposed, they will result in memory leaks

Comment from Sandeep
Time September 2, 2010 at 5:32 am
Some more @ http://spcore.codeplex.com