Connecting to SharePoint and using elevated rights

But let’s start with basic stuff and let’s play with lists a little bit. So, let’s open Visual Studio and create a new … guess what … console application! It is not some bad joke. We need something to run our code and display some information. There is no point to create web parts or custom pages we have to deploy to our server just to get sone output. Console application is also great example about how to write external utilities that interact with SharePoint.

Also add reference to Microsoft.SharePoint assembly. By default it is located in the following folder:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\

Connecting to SharePoint

To access SharePoint data from outside world we have to options – we can use SharePoint API to access SharePoint options and we can use SharePoint web services to access data remotely. In this post we will use SharePoint API to access data locally.

There are two objects we need to use to get access to data. First we make connection to site collection and then we have to ask site we are interested in. There is a little terminological mess you should know. Site Collection is called site on API level (SPSite) and Site is called web (SPWeb). You have to keep in mind this little shift when writing code using SharePoint API.

The following code connects to existing SharePoint site and opens the root web. Then it prints out the titles of all lists it can access.


using System;

using Microsoft.SharePoint;

 

namespace MyTestApp

{

    class Program

    {

        static void Main(string[] args)

        {

            string strUrl = "http://localhost:8099/";

            using (SPSite site = new SPSite(strUrl))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    foreach (SPList list in web.Lists)

                        Console.WriteLine(list.Title);

                }

            }

        }

    }

}


There is one important rule: you must always dispose SharePoint objects soon as they are not used anymore. These objects are very expensive in the mean of resources and the shorter you keep them alive the better performance your code has. The "using" clause does exactly what we need – as soon as object is not used it will be immediately disposed.

Running code in elevated privileges

Everything you with SharePoint objects is done in the context of current user. There are scenarios when you need for some tasks much higher permissions than has the user running the code. You may face this need when writing wev parts for public sites where you want to use some lists or features that are not accessible to anonymous users.

To get our code run using elevated rights we need to add reference to Microsoft.SharePoint.Security assembly. The path of required DLL is as follows:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.Security.dll

Now we can use SPSecurity object that contains everything we need to run code in elevated rights.


using System;

using Microsoft.SharePoint;

 

namespace MyTestApp

{

    class Program

    {

        static void Main(string[] args)

        {

            var sec = new SPSecurity.CodeToRunElevated(

                        delegate() { PrintLists(); });

            SPSecurity.RunWithElevatedPrivileges(sec);

        }

        static void PrintLists()

        {

            string strUrl = "http://localhost:8099/";

            using (SPSite site = new SPSite(strUrl))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    foreach (SPList list in web.Lists)

                        Console.WriteLine(list.Title);

                }

            }

        }

    }

}


Now we can print out our lists also in lower priviligese than thealmighty administrator ones. You have to be very sure what you are doing when running code in elevated privileges because the account used for this is the SharePoint system account and it can do practically everything that can be done on API level.


One thought on “Connecting to SharePoint and using elevated rights

Leave a Reply

Your email address will not be published. Required fields are marked *