2013년 1월 27일 일요일

Get all the views for the SharePoint 2010 list using Client Object Model

Get all the views for the SharePoint 2010 list using Client Object Model




To create list items, you create a ListItemCreationInformation object, set its properties, and pass it as parameter to the AddItem(ListItemCreationInformation) method of theList class. Set properties on the list item object that this method returns, and then call the Update() method, as seen in the following example.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
            oListItem["Title"] = "My New Item!";
            oListItem["Body"] = "Hello World!";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}
Because the previous example creates a standard list item, you do not need to set properties on the ListItemCreationInformation object before it is passed to theAddItem(ListItemCreationInformation) method. However, if your code must create a new folder, for example, you must set the UnderlyingObjectType of theListItemCreationInformation to Folder.
For information and an example about how to create a list item object within the context of the Microsoft SharePoint Foundation 2010 Silverlight object model, see Using the Silverlight Object Model.
To set most list item properties, you can use a column indexer to make an assignment, and call the Update() method so that changes will take effect when you callExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler). The following example sets the title of the third item in the Announcements list.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class UpdateListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.Items.GetById(3);

            oListItem["Title"] = "My Updated Title.";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}
To delete a list item, call the DeleteObject() method on the object. The following example uses the GetItemById() method to return the second item from the list, and then deletes the item.
SharePoint Foundation 2010 maintains the integer IDs of items within collections, even if they have been deleted. So, for example, the second item in a list might not have 2 as its identifier. A ServerException is returned if the DeleteObject() method is called for an item that does not exist.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class DeleteListItem
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
            ListItem oListItem = oList.GetItemById(2);

            oListItem.DeleteObject();

            clientContext.ExecuteQuery(); 
        }
    }
}
If you want to retrieve, for example, the new item count that results from a delete operation, include a call to the Update() method to refresh the list. In addition, you must load either the list object itself or the ItemCount property on the list object before executing the query. If you want to retrieve both a start and end count of the list items, you must execute two queries and return the item count twice, as shown in the following modification of the previous example.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class DeleteListItemDisplayCount
    {
        static void Main()
        {   
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");

            clientContext.Load(oList,
                list => list.ItemCount);

            clientContext.ExecuteQuery();

            int startCount = oList.ItemCount;
            ListItem oListItem = oList.GetItemById(2);

            oListItem.DeleteObject();

            oList.Update();

            clientContext.Load(oList,
                list => list.ItemCount);

            clientContext.ExecuteQuery();

            int endCount = oList.ItemCount;

            Console.WriteLine("Start: {0}  End: {1}", startCount, endCount);
        }
    }
}


http://msdn.microsoft.com/en-us/library/ee539976(v=office.14).aspx




In this article we are going to see how to create, update and delete SharePoint 2010 list items using Client Object Model.

I have created one list named as Test in the SharePoint 2010 site with one column "Title". I will be creating Console Application using Visual Studio 2010 to do create, update and delete methods.

Create List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as CreateListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;namespace CreateListItem
    {
        class Program    {
            static void Main(string[] args)
            {
                string siteUrl = "http://servername:39130/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("Test");
                ListItemCreationInformation listCreationInformation = newListItemCreationInformation();
                ListItem oListItem = oList.AddItem(listCreationInformation);
                oListItem["Title"] = "Item1";
                oListItem.Update();
                clientContext.ExecuteQuery();
            }
        }
    }
  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a new item is added.

    1.gif
Update List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as UpdateListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    namespace
     UpdateListItem
    {
        class Program    {
            static void Main(string[] args)
            {
                string siteUrl = "http://servername:39130/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("Test");
                ListItem oListItem = oList.GetItemById(5);
                oListItem["Title"] = "My Updated Item.";
                oListItem.Update();
                clientContext.ExecuteQuery();
            }
        }
    }
  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a item is updated.
Delete List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as DeleteListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    namespace
     UpdateListItem
    {
        class Program    {
            static void Main(string[] args)
            {
                string siteUrl = "http://servername:39130/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("Test");
                ListItem oListItem = oList.GetItemById(5);         
                oListItem.DeleteObject();
                clientContext.ExecuteQuery();
            }
        }
    }
  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a new item is added.
http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-createupdatedelete-sharepoint-2010-list-items-using-client-object-model/

댓글 없음: