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:

[sourcecode language=”csharp”]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();
}
[/sourcecode]

Add fields to list:

[sourcecode language=”csharp”]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();
}
[/sourcecode]

Creating a new view in a list:

[sourcecode language=”csharp”]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();
}
[/sourcecode]

Creating a new Content Type:

[sourcecode language=”csharp”]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();
}
[/sourcecode]

Adding the existing Site Columns to existing Content Type:

[sourcecode language=”csharp”]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();
}
[/sourcecode]

Save site as template:

[sourcecode language=”csharp”]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();
}
[/sourcecode]

Delete Recycle Bin Items:

[sourcecode language=”csharp”]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(); } [/sourcecode]