About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.
Showing posts with label share point 2010. Show all posts
Showing posts with label share point 2010. Show all posts

Thursday, 12 December 2013

Programatically remove a tab from all the SharePoint 2010 sites

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;
namespace GlobalNavigationRemove
{
class Program
{
static void Main(string[] args)
{
string webAppUrl = string.Empty;
int iCounter = 0;
int iNbrOfSites = 0;
if (args.Length > 0)
{
Console.WriteLine(string.Format("Web Application: {0}", args[0]));
webAppUrl = args[0].ToString();
if (args.Length > 1)
{
Console.WriteLine(string.Format("Test Iteration: {0}", args[1]));
iNbrOfSites = int.Parse(args[1].ToString());
}
}
SPWebApplication webApp = SPWebApplication.Lookup(new Uri(webAppUrl));
Console.WriteLine(string.Format("Total Site collections detected: {0}" , webApp.Sites.Count.ToString()));
Console.WriteLine(string.Format("*************************************Starting the removal ********************************"));

foreach (SPSite siteCollection in webApp.Sites)
{
if (iCounter > iNbrOfSites && iNbrOfSites != 0)
{
return;
}
PublishingWeb myPublishingWeb = PublishingWeb.GetPublishingWeb(siteCollection.OpenWeb());
Console.WriteLine(string.Format("Now Processing : {0}", myPublishingWeb.Title));
SPNavigationNodeCollection publishingNavigationNodes = myPublishingWeb.Navigation.GlobalNavigationNodes;
try
{
SPWeb web = siteCollection.OpenWeb();
SPNavigationNodeCollection webNavigationNodes = web.Navigation.TopNavigationBar;
foreach (SPNavigationNode node in webNavigationNodes)
{
if (node.Title.Trim().ToLower() == "home")
{
node.Delete();
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Exception Occurred while processing site {0} \n\n", ex.ToString()));
}
iCounter++;
}
}
}

Activate a feature in all the SharePoint site collections-PowerShell

$webAppUrl = "Web Application URL"
 

$webapp = Get-SPWebApplication $webAppUrl  | Get-SPSite -Limit All | Get-SPSite -limit 

all | ForEach-Object {Enable-SPFeature -Identity 'Feature guid'  -Url $_.Url}

Wednesday, 11 December 2013

How to implement Impersonation in Sharepoint 2010

Sometime during SharePoint application development when you need your code to perform certain functions that the current user does not have the necessary permissions to perform. The SharePoint SPSecurity class provides a method (RunWithElevatedPrivileges) that allows you to run a subset of code in the context of an account with higher privileges than the current user. 

You need to wrap the RunWithElevatedPrivileges method around your code. And if you are working with Web forms, you may also need to set the AllowUnsafeUpdates property of SPWeb object to true to temporarily turn off security validation within your code. If you use this technique, it is imperative that you set the AllowUnsafeUpdates property back to false to avoid any potential security risks.

Code example :

SPSite objSite = SPContext.Current.Site;
SPWeb objWeb = mySite.OpenWeb();
//Using RunWithElevatedPrivileges
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Get references to the site collection and site for the current context.
   // using statement makes sures these references are disposed properly.
   using (SPSite siteCollection = new objSite(mySite.ID))
   {
      using (SPWeb web = siteCollection.OpenWeb(objWeb.ID))
      {
         web.AllowUnsafeUpdates = true;
         try
         {
            //Your code
         }
         web.AllowUnsafeUpdates = false;
         siteCollection = null;
         web = null;
      }
   }
});

Create custom site definition in SharePoint Server 2010

Introduction

 In this article I am explaining the steps to create a SharePoint 2010 custom site definition by copying an existing site definition. The steps involved in this are not much different then the last versions on SharePoint.You only need to change in two schema files. First file is the copy of WEBTEMP xml file and second is copy of onet.xml file.

Background

For copying existing site definition, you first need to know the location of the existing site definitions. Site definitions are located in the following folder in the SharePoint server 14 hive:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates 
Each site definition has its own sub directory under this folder. The sub directory contains the .aspx pages, images, and one onet.xml file. onet.xml file contains the configuration and modules informations of that particular site definition. WEBTEMP xml file contains the registration information of the Site Definition with SharePoint. Actual name of WEBTEMP file would be like WEBTEMP_<Name of Site Definition>.xml. WEBTEMP file are located in the following location:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\1033\XML

Steps Involved

1. Clone an existing site definition folder located in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\ directory.

2. Give it any name that contains no spaces. For example, to create a custom site definition that derives from the blank site definition for Windows SharePoint Services, copy the \sts folder.
3. Rename the copy of sts folder to MyTestSite.

4. Make a copy of the WebTemp.xml file that is located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\1033\XML.

5. Rename the copied file to WEBTEMP_MyTestSite.xml

6. Open WEBTEMP_MyTestSite.xml using Vishal Studio 2010 or some other editor to do the customization. It contains a collection of Template elements and Configuration subelements. The Configuration element defines the title, description, URL for the image displayed in the user interface, and a display category that specifies the tab on which to display the template in the Template Selection section of the New SharePoint Site page. "My Custom Site Definition" in this example.
<?xml version="1.0" encoding="utf-8"?>

   <Templates xmlns:ows="Microsoft SharePoint">

      <Template Name="MyTestSite" ID="10005">

         <Configuration ID="0" Title="MyTestSite Template" Hidden="FALSE" ImageUrl="" 
            Description="This template is used for initializing a new site." 
            DisplayCategory="My Custom Site Definition"></Configuration>

      </Template>

   </Templates>

Note: In each Template element defined in the WebTemp file, the Name attribute must contain the same name that is assigned to the new folder. In this example it is "MyTestSite". Also, to avoid conflict with IDs already used in Windows SharePoint Services, use unique values greater than 10,000 for the ID attribute. I am using ID = 10005 in this example.
7. Reset the IIS. (Open command prompt. Type iisreset and press enter key.)

8. Open Central Administration and goto Application Management -> Create Site Collection. A new tab will appear.

9. Select the site template you created. Enter Title and Url and click on Create button.

Your new sharepoint site is created.