Archive for the ‘SharePoint’ Category

Execute SharePoint Power shell commands from C# code

July 25, 2010

A friend of mine asked me yesterday how can we run a SharePoint power shell command like backup a site collection from a C# windows application and i decided to give it a try and here are the steps on how can we do it.

1. Add a reference to “System.Management.Automation” assembly in your project. I could not locate the assembly and a bit goggling suggested that add the following xml to your csproj file by editing it directly.

<Reference Include="System.Management.Automation" />

2. Add a using statement for following namespace:

using System.Management.Automation

3. Call following method on your button click event. This method executes “backup-spsite” command to backup a site collection. Modify the method by changing your site collection URL and backup file path and you are good to go.

  private void ExecutePowerShell()
            PowerShell OPowerShell = null;
            Runspace OSPRunSpace = null;

                RunspaceConfiguration OSPRSConfiguration = RunspaceConfiguration.Create();
                PSSnapInException OExSnapIn = null;

                //Add a snap in for SharePoint. This will include all the power shell commands for SharePoint
                PSSnapInInfo OSnapInInfo = OSPRSConfiguration.AddPSSnapIn(“Microsoft.SharePoint.PowerShell”, out OExSnapIn);

                OSPRunSpace = RunspaceFactory.CreateRunspace(OSPRSConfiguration);

                OPowerShell = PowerShell.Create();

                OPowerShell.Runspace = OSPRunSpace;

                Command Cmd1 = new Command(“backup-spsite”);
                Cmd1.Parameters.Add(“identity”, “Your Site Coll URL”);
                Cmd1.Parameters.Add(“path”, “Back up file path”);




            catch (Exception Ex)
                //Handle exception
                if (OSPRunSpace != null)
                    OSPRunSpace = null;

                if (OPowerShell != null)
                    OPowerShell = null;

Power shell is a wide subject and there are many things which you can do like executing the commands on remote machine, writing your own commands etc. I’ll write down about these in my future posts.

Lock Down Feature for WSS

December 31, 2009

When we setup the anonymous access for the SharePoint site, all anonymous user gets the limited access to the site. Because of the limited access to the site, users are able to view the list form pages like AllItems.aspx, DispForms.aspx etc.

MOSS provides a feature called ViewFormPagesLockDown which allow us to secure these form pages from anonymous users. The following table details the default permissions of the limited access permission level and the reduced permissions when lockdown mode is turned on.


Limited access — default

Limited access — lockdown mode

List permissions: View Application Pages

Site permissions: Browse User Information

Site permissions: Use Remote Interfaces

Site permissions: Use Client Integration Features

Site permissions: Open

Unfortunately this feature comes along with MOSS only and is not the part of WSS. So how can we achieve the similar functionality in WSS?

What about creating our own Lock Down feature?

Yes, I developed the custom Lock down feature to achieve this. Following is the code i used to suppress the permissions of Limited Access permission level.

private void UpdateLimitedAccess(bool LockDown, string StrSiteCollURL)
               SPSite Site = new SPSite(StrSiteCollURL);
               Site.AllowUnsafeUpdates = true;
               SPWeb RootWeb = Site.RootWeb;
               RootWeb.AllowUnsafeUpdates = true;
               SPRoleDefinition ByType = RootWeb.RoleDefinitions.GetByType(SPRoleType.Guest);
               if (!LockDown)
                   ByType.BasePermissions |= SPBasePermissions.EmptyMask | SPBasePermissions.ViewFormPages;
                   ByType.BasePermissions |= SPBasePermissions.UseRemoteAPIs;
                   ByType.BasePermissions &= ~(SPBasePermissions.EmptyMask | SPBasePermissions.ViewFormPages);
                   ByType.BasePermissions &= ~SPBasePermissions.UseRemoteAPIs;

Call this method in your feature activation event passing true as parameter value and in feature deactivation event passing false as parameter value.

Create a user friendly URL for your SharePoint site

August 9, 2009

Often we need to create a user friendly URL for our SharePoint site like

Following are the major activities needed to be performed to achieve this:

  1. Create a DNS entry
  2. Add an Alternate access mapping
  3. Set the host header in IIS web application

Create a DNS entry

Add a record for in the DNS server. Your IT administrator should be able to do it for you.

However, a DNS Server is not necessary. The hosts file can also be used, but that means the hosts file must be edited on each client.

Host file path:  “%WinDir%\system32\drivers\etc”

Add an Alternate access mapping:

  1. Open Central Administration site.
  2. Click on Operations tab.
  3. Under “Global Configuration” section, click on “Alternate access mappings”.
  4. 1

  5. Click on “Show All” menu in the right and then click on “Change Alternate Access Mapping Collection”.
  6. 2

  7. Select the web application for which you wish to create a user friendly URL. In my case the name of the Web App is DemoApp.
  8. 3

  9. Click on “Edit Public URLs”.
  10. 4

  11. In the “Public URLs” section, in “Default” text box, enter the fully qualified URL for the web application. In my case it is In the “Intranet” text box enter the original URL of the web application.
  12. 5

  13. Click  save.

Set the host header in IIS web application

Note: This activity is to be performed on all the front end servers.

  1. Open IIS.
  2. Right click on the web application and select properties. In my case, the Web App name is DemoApp.
  3. 6

  4. Click on “Advanced…”.
  5. 7

  6. Click on Add”.
  7. 8

  8. Fill the  TCP port and Host Header Value as shown below and click OK.
  9. 9

  10. Your screen should like as shown in the screenshot below.
  11. 10

    Note: If you have your site at port 80 in that case you need not add a new identity. Instead you can edit the existing entry and specify the host header value for it. A screenshot of that is below:


  12. Click Ok and apply.
  13. Restart IIS.

You should be able to access your site now using the URL