Saturday, June 14, 2014

Using Server object model SharePoint 2013

The below console application shows the common operations using the SharePoint 2013 Server object model.


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

namespace DemoConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //DisplayListItems();
            //DisplayItemById(1);
            //QueryList();
            //CreateList();
            //DisplayPagesLib();
            //AddListFields();
            //AddSiteContentType();
            //AddView();
            AddListItem();
            Console.ReadLine();
        }
        private static void DisplayListItems()
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList empList = web.GetList("/Lists/EmpInfo");
                        SPListItemCollection items = empList.Items;
                        if (null == items)
                        {
                            return;
                        }
                        foreach (SPListItem item in items)
                        {
                            Console.WriteLine(item.Title);
                        }

                    }
                }

            }
            catch (SPException spe)
            {
                Console.WriteLine(spe.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private static void DisplayItemById(int id)
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList empList = web.GetList("/Lists/EmpInfo");
                        SPListItem item = empList.GetItemById(id);
                        if (null == item)
                        {
                            Console.WriteLine("Item with this id: {0} doesn't exists", id);
                            return;
                        }
                        Console.WriteLine(item.Title);
                        Console.WriteLine(item["EmpFirstName"]);
                        Console.WriteLine(item.ID);
                        Console.WriteLine(item["EmpLastName"]);
                    }
                }

            }
            catch (SPException spe)
            {
                Console.WriteLine(spe.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private static void QueryList()
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList empList = web.GetList("/Lists/EmpInfo");
                        SPQuery query = new SPQuery();
                        query.RowLimit = 100;
                        query.Query = "<Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Eq><Eq><FieldRef Name='ID' /><Value Type='Counter'>2</Value></Eq></Or></Where>";
                        SPListItemCollection items = empList.GetItems(query);
                        foreach (SPListItem item in items)
                        {
                            Console.WriteLine("item : " + item.ID);
                            Console.WriteLine(item.Title);
                            Console.WriteLine(item["EmpFirstName"]);
                            Console.WriteLine(item["EmpLastName"]);
                        }

                    }
                }

            }
            catch (SPException spe)
            {
                Console.WriteLine(spe.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private static void CreateList()
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        Guid listId = web.Lists.Add("DemoCustomList", "DemoCustomList", SPListTemplateType.GenericList);
                        SPList list = web.Lists[listId];
                        list.ContentTypesEnabled = true;

                        list.EnableAttachments = false;
                        list.EnableVersioning = true;
                        list.Update();


                    }
                }
            }
            catch (Exception ex) { }
        }
        private static void AddListFields()
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList demoList = web.GetList("/Lists/DemoCustomList");
                        //demoList.Fields.Add("DemoCol", SPFieldType.Text, false);
                        //demoList.Fields.Add("DemoChoice", SPFieldType.Choice, false);
                        //SPFieldChoice choiceField = demoList.Fields["DemoChoice"] as SPFieldChoice;
                        //choiceField.AddChoice("DemoChoice1");
                        //choiceField.AddChoice("DemoChoice2");
                        //choiceField.AddChoice("DemoChoice3");
                        //choiceField.AddChoice("DemoChoice4");
                        //choiceField.Update();
                        Guid empInfoListId = web.GetList("/Lists/Empinfo").ID;
                        demoList.Fields.AddLookup("DemoLkpCol", empInfoListId, false);

                        SPFieldLookup lkpDemoLookup = demoList.Fields["DemoLkpCol"] as SPFieldLookup;
                        lkpDemoLookup.LookupField = "Title";
                        lkpDemoLookup.Update();


                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
        private static void DisplayPagesLib()
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPDocumentLibrary pagesLib = web.GetList("/Pages") as SPDocumentLibrary;
                        SPListItemCollection pagesColl = pagesLib.Items;
                        foreach (SPListItem item in pagesColl)
                        {
                            Console.WriteLine("Title: " + item.Title + "           Url: " + SPUrlUtility.CombineUrl(web.Url, item.Url));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void AddSiteContentType()
        {
            try
            {
                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        //web.Fields.Add("DemoSiteCol1", SPFieldType.Text, false);
                        //StringCollection choiceColl = new StringCollection();
                        //choiceColl.Add("DemoSiteCol2Choice1");
                        //choiceColl.Add("DemoSiteCol2Choice2");
                        //choiceColl.Add("DemoSiteCol2Choice3");
                        //choiceColl.Add("DemoSiteCol2Choice4");
                        //web.Fields.Add("DemoSiteCol2", SPFieldType.Choice, false, false, choiceColl);

                        //SPField demoCol1 = web.Fields["DemoSiteCol1"];
                        //SPField demoCol2 = web.Fields["DemoSiteCol2"];
                        //SPContentType itemCT = web.ContentTypes["Item"];
                        //SPContentType demoCt = new SPContentType(itemCT, web.ContentTypes, "DemoCT1");
                        //web.ContentTypes.Add(demoCt);


                        //SPField field1 = web.Fields["DemoSiteCol1"];
                        //SPField field2 = web.Fields["DemoSiteCol2"];

                        //SPFieldLink link1 = new SPFieldLink(field1);
                        //SPFieldLink link2 = new SPFieldLink(field2);

                        //SPContentType demoCT1 = web.ContentTypes["DemoCT1"];
                        //demoCT1.FieldLinks.Add(link1);
                        //demoCT1.FieldLinks.Add(link2);
                        //demoCT1.Update();


                        SPContentType demoSiteCT = web.ContentTypes["DemoCT1"];

                        SPList demoList = web.GetList("/Lists/DemoCustomList");
                        demoList.ContentTypes.Add(demoSiteCT);
                        demoList.Update();

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private static void AddView()
        {
            try
            {

                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {


                        SPList demoList = web.GetList("/Lists/DemoCustomList");
                        //StringCollection viewFields = new StringCollection();
                        //viewFields.Add("DemoSiteCol1");
                        //viewFields.Add("DemoSiteCol2");
                        //viewFields.Add("Title");
                        //viewFields.Add("ID");
                        //SPView view = demoList.Views.Add("Demoview", viewFields, "", 30, true, true);

                        SPView demoView = demoList.Views["Demoview"];
                        demoView.Query = "<OrderBy><FieldRef Name='DemoSiteCol1' /></OrderBy>";
                        demoView.Update();

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void AddListItem()
        {
            try
            {

                using (SPSite site = new SPSite(@"<<siteUrl>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList demoList = web.GetList("/Lists/DemoCustomList");
                    SPListItem item= demoList.Items.Add();
                    item["Title"] = "demo123";
                    item["DemoSiteCol1"] = "DemoSiteCol1123412";
                    item["DemoSiteCol2"] = "DemoSiteCol2Choice4";
                    item.Update();

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

the push notifications feature is not activated on the site sharepoint 2013

the push notifications feature is not activated on the site sharepoint 2013 


You sometimes get the below error message when you try to use SharePoint 2013 managed client object model
"the push notifications feature is not activated on the site "

You need to the below powershell command to enable the feature related to this.


Enable-SPFeature -Identity 41e1d4bf-b1a2-47f7-ab80-d5d6cbba3092 -URL "<<site collection Url>>"

Thursday, June 12, 2014

SharePoint SecurityTrimmedControl usage

<SharePoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControl2" runat="server"
            PermissionsString="AddListItems">
<!-- Controls to allow. Only those who are able to add the list items/documents can access these controls  -->
</SharePoint:SPSecurityTrimmedControl>

Here is the full list of permissions.

Member name
Description
EmptyMask
Has no permissions on the Web site. Not available through the user interface.
ViewListItems
View items in lists, documents in document libraries, and view Web discussion comments.
AddListItems
Add items to lists, add documents to document libraries, and add Web discussion comments.
EditListItems
Edit items in lists, edit documents in document libraries, edit Web discussion comments in documents, and customize Web Part Pages in document libraries.
DeleteListItems
Delete items from a list, documents from a document library, and Web discussion comments in documents.
ApproveItems
Approve a minor version of a list item or document.
OpenItems
View the source of documents with server-side file handlers.
ViewVersions
View past versions of a list item or document.
DeleteVersions
Delete past versions of a list item or document.
CancelCheckout
Discard or check in a document which is checked out to another user.
ManagePersonalViews
Create, change, and delete personal views of lists.
ManageLists
Create and delete lists, add or remove columns in a list, and add or remove public views of a list.
ViewFormPages
View forms, views, and application pages, and enumerate lists.
Open
Allow users to open a Web site, list, or folder to access items inside that container.
ViewPages
View pages in a Web site.
AddAndCustomizePages
Add, change, or delete HTML pages or Web Part Pages, and edit the Web site using a SharePoint Foundation–compatible editor.
ApplyThemeAndBorder
Apply a theme or borders to the entire Web site.
ApplyStyleSheets
Apply a style sheet (.css file) to the Web site.
ViewUsageData
View reports on Web site usage.
CreateSSCSite
Create a Web site using Self-Service Site Creation.
ManageSubwebs
Create subsites such as team sites, Meeting Workspace sites, and Document Workspace sites.
CreateGroups
Create a group of users that can be used anywhere within the site collection.
ManagePermissions
Create and change permission levels on the Web site and assign permissions to users and groups.
BrowseDirectories
Enumerate files and folders in a Web site using Microsoft Office SharePoint Designer 2007 and WebDAV interfaces.
BrowseUserInfo
View information about users of the Web site.
AddDelPrivateWebParts
Add or remove personal Web Parts on a Web Part Page.
UpdatePersonalWebParts
Update Web Parts to display personalized information.
ManageWeb
Grant the ability to perform all administration tasks for the Web site as well as manage content. Activate, deactivate, or edit properties of Web site scoped Features through the object model or through the user interface (UI). When granted on the root Web site of a site collection, activate, deactivate, or edit properties of site collection scoped Features through the object model. To browse to the Site Collection Features page and activate or deactivate site collection scoped Features through the UI, you must be a site collection administrator.
UseClientIntegration
Use features that launch client applications; otherwise, users must work on documents locally and upload changes. 
UseRemoteAPIs
Use SOAP, WebDAV, or Microsoft Office SharePoint Designer 2007 interfaces to access the Web site.
ManageAlerts
Manage alerts for all users of the Web site.
CreateAlerts
Create e-mail alerts.
EditMyUserInfo
Allows a user to change his or her user information, such as adding a picture.
EnumeratePermissions
Enumerate permissions on the Web site, list, folder, document, or list item.
FullMask
Has all permissions on the Web site. Not available through the user interface.