Showing posts with label 2010. Show all posts
Showing posts with label 2010. Show all posts

Monday, 6 May 2013

Creating a ContentType with new Site Field in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces
Step 3: Write the following code.

using System;

//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace CreatingContentTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;
            clientContext.Load(site);
            clientContext.ExecuteQuery();

            //Getting content type name
            Console.Write("ContentType Name: ");
            string contentTypeName = Console.ReadLine();
        }
        static void CreateContentType(Web site, ClientContext clientContext, string contentTypeName)
        {
            ContentTypeCollection contentTypeCollection = site.AvailableContentTypes;
            clientContext.Load(contentTypeCollection);
            clientContext.ExecuteQuery();
            int count = 0;

            //Checking whether the ContentType already exists
            foreach (ContentType item in contentTypeCollection)
            {
                clientContext.Load(item);
                clientContext.ExecuteQuery();
                if (item.Name.ToUpper() == contentTypeName.ToUpper())
                    count++;
            }

            //If doesnot exist, create it
            if (count == 0)
            {
                Console.Write("SiteField Name: ");
                string siteFieldName = Console.ReadLine();
                string siteField =
                    "<Field Type = 'Text' " +
                    "Hidden = 'False' " +
                    "DisplayName = '" + siteFieldName + "' " +
                    "ResultType = 'Text' " +
                    "ReadOnly = 'False' " +
                    "Name = '" + siteFieldName + "' " +
                    "Group = 'Custom Columns'></Field>";
                Field field = site.Fields.AddFieldAsXml(siteField, false, AddFieldOptions.AddFieldToDefaultView);
                clientContext.ExecuteQuery();
                FieldLinkCreationInformation fieldLink = new FieldLinkCreationInformation();
                fieldLink.Field = field;

                //Creating ContentType
                ContentTypeCreationInformation contentType = new ContentTypeCreationInformation();
                contentType.Name = contentTypeName;
                contentType.Group = "SampleGroup";
                contentType.ParentContentType = contentTypeCollection[0].Parent;
                ContentType myContentType = site.ContentTypes.Add(contentType);

                //Adding fields to ContentType
                myContentType.FieldLinks.Add(fieldLink);
                field.Update();
                myContentType.Update(true);
                clientContext.ExecuteQuery();
                site.Update();
                Console.WriteLine("ContentType Created Successfully");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("ContentType already presents in the site.");
                Console.Write("Enter another ContentType name: ");
                string content = Console.ReadLine();
                CreateContentType(site, clientContext, content);
            }
        }
    }
}

Wednesday, 30 January 2013

Updating Items in List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace UpdatingListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");

            //Getting List item
            ListItem listItem = list.GetItemById(2);

            //Updating List item
            listItem["First Name"] = "Hrithik";
            listItem["Last Name"] = "Roshan";
            listItem["Age"] = 37;
            listItem.Update();

            clientContext.ExecuteQuery();
            Console.WriteLine("List Item Updated Successfully");
            Console.ReadLine();
        }
    }
}


Filtering Items from List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace FilteringListItems
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");
            clientContext.Load(list);
            clientContext.ExecuteQuery();

            //Filtering List Items
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
                @"<View>
                    <Query>
                      <Where>
                        <Eq>
                          <FieldRef Name='Age' />
                          <Value Type='Number'>35</Value>
                        </Eq>
                      </Where>
                    </Query>
                  </View>";
            ListItemCollection listItemCollection = list.GetItems(camlQuery);
            clientContext.Load(listItemCollection);
            clientContext.ExecuteQuery();

            //Printing List Items
            foreach (ListItem listItem in listItemCollection)
            {
                Console.WriteLine("Title: {0}, First Name: {1}, Last Name: {2}, Age: {3}", listItem["Title"], listItem["First Name"], listItem["Last Name"], listItem["Age"]);
            }
            Console.ReadLine();
        }
    }
}


Deleting Items from List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace DeletingListItems
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");

            //Deleting List Item
            ListItem listItem = list.GetItemById(2);
            listItem.DeleteObject();
            clientContext.ExecuteQuery();

            Console.WriteLine("List Item Deleted Successfully");
            Console.ReadLine();
        }
    }
}


Adding Items to List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace AddItemsToList
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");

            //Adding Items to List
            ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();

            ListItem listItem = list.AddItem(listItemCreationInformation);
            listItem["Title"] = "1";
            listItem["First Name"] = "John";
            listItem["Last Name"] = "Abraham";
            listItem["Age"] = 35;
            listItem.Update();

            listItem = list.AddItem(listItemCreationInformation);
            listItem["Title"] = "2";
            listItem["First Name"] = "Sharukh";
            listItem["Last Name"] = "Khan";
            listItem["Age"] = 40;
            listItem.Update();

            clientContext.ExecuteQuery();
            Console.WriteLine("Items Added Successfully To List");
            Console.ReadLine();
        }
    }
}

Filtering List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace FilteringListByContentType
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Filtering List by Content Type
            ListCollection listCollection = site.Lists;
            IEnumerable<List> filteredLists = clientContext.LoadQuery(listCollection.Where(list => list.BaseType == BaseType.DocumentLibrary));
            clientContext.ExecuteQuery();

            //Printing List
            foreach (List li in listCollection)
            {
                Console.WriteLine("Title: {0}", li.Title);
            }
            Console.ReadLine();
        }
    }
}


Getting Column Names (List) from SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespaces
using Microsoft.SharePoint.Client;

namespace GettingFields
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");
            clientContext.Load(list);
            clientContext.ExecuteQuery();

            //Getting Column Names
            FieldCollection fieldCollection = list.Fields;
            clientContext.Load(fieldCollection);
            clientContext.ExecuteQuery();

            //Printing Column Names
            foreach (Field field in fieldCollection)
            {
                Console.WriteLine("Name: " + field.InternalName.ToString());
            }
            Console.ReadLine();
        }
    }
}