SharePoint Events

  6/25/2013 - Webcast: ROI of Workflows in SharePoint
  6/26/2013 - Webcast: SharePoint 2013 and ECM: Document Generation & Assembly
  6/27/2013 - Webcast: SharePoint 2013 and ECM: Document Processing & Content Standardization
  6/28/2013 - Webcast: RFI and Submittal Management in SharePoint
  7/25/2013 - Webcast: AEC Document Management and Distribution in SharePoint
  8/25/2013 - Conference: SharePoint Fest 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 and Enterprise Content Management
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  What's New in Search for SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View
  Managing CAD in SharePoint
  SharePoint Document Automation and E-Forms for Financial Services

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Using the SharePoint Object Model from a Console Application

By: Robert Christ

This post is part 1 of a 2 part series

Part 2 can be found at: http://www.thesharepointblog.net/Lists/Posts/Post.aspx?ID=75 and takes this example, and implements it in a program called “Update All Site Collection Administrators In Farm.”

Traditional Manners of Manipulating and configuring SharePoint

When it comes to configuring a SharePoint farm or solution, there are a number of tools available to the modern SharePoint developer, each of which has its own strengths and weaknesses.

·     The UI – Great for simple tasks, and giving visual context to the SharePoint task

·     Stsadm – Well tested tool for manipulating SharePoint configurations one at a time, or through bat files.

·     PowerShell – A great improvement over stsadm, allows for a much larger degree of flexibility and complexity

·     SharePoint COM (Client Object Model) – Allows code manipulation of the farm from an external machine.

Today I would like to remind the SharePoint community of a fifth option:

·     Manipulating SharePoint directly through a Console Application

Why would I bother with this option?

As developers, we like to reuse code whenever available.  Tasks that are complicated, repetitive, or time consuming should be automated to save users, and developers’ time.

Of the first 4 options, if you have a complicated task, your best bet would be to write a reusable PowerShell script.  This is a great option, and something the SharePoint community has embraced as a whole.

What I haven’t seen much of however, is the alternative option of directly manipulating SharePoint through the use of Console Applications.  While it is possible this is generally taken as a given by the SharePoint community, I thought that perhaps creating a brief tutorial might be helpful for some of you out there.  Unlike PowerShell, even the most basic SharePoint developer most likely has an intimate knowledge of the SharePoint object model, making this an easy option. 

Far more importantly, this technique can be used as a test harness for any and all workflow, event handler, or timer job code, as it will directly interact with SharePoint, but without the (often considerable) extra lag time involved with debugging directly in or debug/deploying to SharePoint! 

How to Manipulate SharePoint using the Object Model from a Console Application

Hopefully I will be able to load a pre-made project to this site for you to download, but just in case, I’ve listed the steps to create this project below.  You should only ever have to do this once, as it will always be the same.  Simply change the method call exampleMethod() to whatever you will use to manipulate SharePoint normally.

First, open up Visual Studio, and create a new Console Application Project using .Net 3.5.

The_SharePoint_Blog_Using_the_SharePoint_Object_Model_from_a_Console_Application

Next, click Project in the Visual Studio drop down menus, and choose “Your Console Application’s Name Here” Properties.

The_SharePoint_Blog_Using_the_SharePoint_Object_Model_from_a_Console_Application

Be sure to set the Build Platform Target to x64.  Failure to do this step will, of course, result in an immediate error upon program launch on your x64 SharePoint server.

Next, right click References in the Solution Menu, and Add a reference to Microsoft.SharePoint , as you would to use the SharePoint Object Model in a workflow or event receiver.

The_SharePoint_Blog_Using_the_SharePoint_Object_Model_from_a_Console_Application

Now open Program.cs, and add the following statements to the top of the file:

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

using System.Reflection;

 

Coding using the Object Model from a Local Console Application

In your main method, the very first thing you want to call is

static void Main(string[] args)

{

Assembly SharePoint;

       bool correctMachine = checkSharePointComputer(out SharePoint);

if (!correctMachine)

              return;

 

runSharePointScriptWithElevatedPrivliges();

 }

 

Where checkSharePointComputer(out SharePoint) is defined as:

/// <summary>

/// Determines if SharePoint 2010 is installed on this computer

/// </summary>

/// <param name="SharePoint">The DLL file for Microsoft.SharePoint</param>

/// <returns>True or false depending on whether Microsoft.SharePoint was

/// found on the computer</returns>

private static bool checkSharePointComputer(out Assembly SharePoint)

{

try

       {

SharePoint = Assembly.Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=Neutral, PublicKeyToken=71e9bce111e9429c");

              return true;

          }

          catch

          {

                Console.WriteLine("Unfortunately, SharePoint 2010 is not installed on "

+ "this machine.");

                Console.WriteLine("This program must be run on a x64 OS with "

+ "SharePoint Server 2010 installed.");

                Console.WriteLine("Enter any key to now quit.");

                Console.ReadLine();

                SharePoint = null;

                return false;

          }

    }

 

Hopefully this function is self-explanatory, but just in case, checkSharePoitnComputer will search the local computer for a reference to Microsoft.SharePoint.  Normally when running an application, if at runtime the computer cannot find the reference to Microsoft.SharePoint, the application will fail due to missing dependencies.  By including this method in our project however, we simultaneously ensure that the console application will only be run on the computers with SharePoint installed (the machine to manipulate), and that it will fail gracefully if a user accidentally attempts to run it elsewhere.

Similarly, runSharePointScriptWithElevatedPrivliges should be defined as:

/// <summary>

/// Runs SharePoint code with elevated privliges

/// </summary>

private static void runSharePointScriptWithElevatedPrivliges

(string url)

{

SPSecurity.RunWithElevatedPrivileges(delegate()

       {

              string result = exampleMethod(url);

              Console.WriteLine(result);

       });

}

 

where exampleMethod is can be pointed towards whatever code you would like to manipulate SharePoint with. 

Remember, unlike when you run SharePoint code as a workflow or event handler, the code will be run as your local user, not the system or farm account.  As such, you will almost certainly need to call your code from within this elevated privileges delegate.  Be careful!

If you’ve gotten this far:

… then you’ve only got one last step to go.  You’ll find that attempting to get an instance of SPSite or SPWeb in the traditional manner:

using (SPSite site = new SPSite(url))

{

using (SPWeb web = site.OpenWeb())

       {

       }

}

will not work from within a console application.  Instead, you need to use:

//Opens Web application.  Opening directly using SPSite() is not available in exe files

SPWebApplication webApp;

try

{

Uri b = new Uri(url);

       webApp = SPWebApplication.Lookup(b);

}

catch (Exception ex)

{

return "Could not find the specified url" + ex.Message;

}

using (SPSite site = webApp.Sites[0])

{

using (SPWeb web = site.OpenWeb())

       {

       }

}

 

There you go!  Now you can manipulate SharePoint exactly the same as if you were doing so from within a workflow, event receiver, Stsadm or PowerShell, but in a significantly faster manner than available with those first three options.

If you’re interested in seeing a basic example of this code in action, scroll to the top of the page, and follow the link to updateAllSiteCollectionAdministrators. 

The entire code for this solution has been copy pasted below.  Enjoy!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

using System.Reflection;

 

namespace SiteCollectionAdminChanger

{

    class Program

    {

        static void Main(string[] args)

        {

            Assembly SharePoint;

            bool correctMachine = checkSharePointComputer(out SharePoint);

 

            if (!correctMachine)

                return;

            string url = "YOURURLHERE";

            runSharePointScriptWithElevatedPrivliges(url);

 

            Console.ReadLine();

        }

 

        /// <summary>

        /// Determines if SharePoint 2010 is installed on this computer

        /// </summary>

        /// <param name="SharePoint">The DLL file for Microsoft.SharePoint</param>

        /// <returns>True or false depending on whether Microsoft.SharePoint was

 /// found on the computer</returns>

        private static bool checkSharePointComputer(out Assembly SharePoint)

        {

            try

            {

                SharePoint = Assembly.Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=Neutral, PublicKeyToken=71e9bce111e9429c");

                return true;

            }

            catch

            {

                Console.WriteLine("Unfortunately, SharePoint 2010 is not installed "

                            + "on this machine.");

                Console.WriteLine("This program must be run on a x64 OS with "

                            + "SharePoint Server 2010 installed.");

                Console.WriteLine("Enter any key to now quit.");

                Console.ReadLine();

                SharePoint = null;

                return false;

            }

        }

 

        /// <summary>

        /// Runs sharepoitn code with elevated privliges

        /// </summary>

        private static void runSharePointScriptWithElevatedPrivliges(string url)

        {

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                string result = exampleMethod(url);

                Console.WriteLine(result);

            });

        }

 

        private static string exampleMethod(string url)

        {

 

            //Opens Web application.  Opening directly using SPSite() is not available in exe files

            //unless using Microsoft.SharePoint.Client

            SPWebApplication webApp;

            try

            {

                Uri b = new Uri(url);

                webApp = SPWebApplication.Lookup(b);

            }

            catch (Exception ex)

            {

                return "Could not find the specified url  " + ex.Message;

            }

 

            using (SPSite site = webApp.Sites[0])

            {

                using (SPWeb web = site.OpenWeb())

                {

                    return "success!";

                }

            }

        }

    }

}

 

By: Robert Christ

        

Comments

Udayan

Intelligent Post .But  will it work if run this code from different Domain..

Thanks,
Udayan
at 11/6/2012 8:41 AM

Robert

The code displayed here must be run from the SP server, and the main SP domain required credentials.  If you wish to run the code from off the SP server, you need to use the Client Object Model.
WDC812\richardm at 11/15/2012 1:15 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy