Wednesday, 6 March 2013

ADD, Update and Delete SharePoint List Item Programmatically


Using Microsoft SharePoint Server Object Model we can easily Add, Update and Delete list items programmatically. Below is a code in C# .Net For Add, Update and Delete operations..
using (SPSite oSPsite = new SPSite("http://website url/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {
            oSPWeb.AllowUnsafeUpdates = true;
            // Fetch the List
            SPList list = oSPWeb.Lists["MyList"];
                   
            //Add a new item in the List
            SPListItem itemToAdd = list.Items.Add();
            itemToAdd["Title"] = "Test Title";
            itemToAdd["Description"] = "Test Description";
            itemToAdd.Update();
            // Get the Item ID
            listItemId = itemToAdd.ID;
            // Update the List item by ID
            SPListItem itemToUpdate = list.GetItemById(listItemId);
            itemToUpdate["Description"] = "Changed Description";
            itemToUpdate.Update();
            // Delete List item
            SPListItem itemToDelete = list.GetItemById(listItemId);
            itemToDelete.Delete();
            oSPWeb.AllowUnsafeUpdates = false;
       }
}

Sunday, 10 February 2013

Create SharePoint 2010 Site Collection Programmatically


·        First Create Empty SharePoint Project in visual studio.


Deploy as farm Solution.


In the project add services Reference of admin.asmx
·        Right click on project and click Add Services Reference.

 Click Advanced

·        Then Click Add web Reference


 ·       Then enter the Central Administrator admin.asmx service.
·        To get the URL of admin.asmx use the following steps.
·        To get the services URL of Central admin’s admin.asmx.
o   Go to IIS Click sites,
o   Then click to SharePoint Central Administration v4,
o   Click _vti_bin and click on Content View.
o   Then browse the admin.asmx.
 


    Copy the URL.

·        Then use that copied URL.


 ·        Then click Go. Change the web reference name and click Add Reference.

 
·        Then add following Line of code to create Site collection.


Site URL is Managed path.
Site Collection Name: - Name of new site collection.
Title: - Display Name of Site.
Description: -Description about site.
1033