Wednesday, October 5, 2016

Create, Update, Delete a List Item Using COM

Using C# console


Add reference to dll: Microsoft.SharePoint, Microsoft.SharePoint.Client, Microsoft.SharePoint.Client.Runtime

For Create action:


using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicOperationUsingCSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://web";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("MyCustomList");
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
            oListItem["Title"] = "My New Item!";
            oListItem["Description"] = "Hello World!";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}

For Update:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicOperationUsingCSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://web";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("MyCustomList");
            ListItem oListItem = oList.GetItemById(1);
            oListItem["Title"] = "Hello World Updated!!!";
            oListItem["Description"] = "Hello World Updated!!!";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}


For Delete:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicOperationUsingCSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://web";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("MyCustomList");
            ListItem oListItem = oList.GetItemById(1);
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}


0 comments:

Post a Comment