Friday, February 28, 2014

Error Type: The feature is not farm level feature and is not found in a site level defined by the url.

I have one .wsp file which is created and deployed through visual studio in development environment.
Now am trying to move that .wsp file to testing/production environment.
I have taken one .wsp file and added/installed the .wsp in testing/production environment using powershell.
Now i tried to activate the feature with the following command
Enable-SPFeature -Identity "AddLinkToSiteSettings Feature1" -URL "http://dev01:9999"
but am get the following error
"The Feature is not a Farm Level Feature and is not found in a Site level defined by the Url"

However I can see the feature in site settings/site feature page.

Solution for this is use feature id instead of feature name.

Enable-SPFeature -Identity e4f775d5-e927-4da5-8f92-7f7ce49c3703 -URL "http://dev01:9999"

Thats it...

Monday, February 24, 2014

Difference between Backup/Restore and Export/Import


Backup-SPSite is generally used when you want to replicate the entire site collection (including all subsites) to an existing web application.

It allows you to backup either a site collection or web application.
You can basically consider the file generated more or less as a SQL dump of (a part of) your content database.It preserves the GUID of every object except the GUID of the Site.
When you restore the backup, SharePoint generates a new GUID for the site collection.

Export-SPWeb is generally used when you want to replicate just a single subsite to an existing site collection, that allows you to backup data of a sub site (SPWeb object), but it can also export a site collection, an entire web application, or a single list. It generates a new GUID for every objects such as sites, sub sites, lists and items.

A major drawback of this operation is that it does not preserves workflows instances, associations, history and tasks.Every workflow association must be recreated and there is no way to restore the running instances from original site.

Export/import is often used to split site collections into multiple pieces when they reached a certain limit. Or to do the vice versa and consolidate multiple site collections into one larger one. Both of these actions work fine as long as the migrated content does not use the publishing feature.

Example for Subsite backup and restore:

Export-SPWeb -Identity http://spsdemo:9999/Subsite1 -Path "D:\Exports\Subsite1"
Import-SPWeb http://spsdemo:6666/Subsite2 -Path "D:\Exports\Subsite1.cmp"

Example for list backup and restore:

Export-SPWeb http://spsdemo:9999/ -ItemUrl "/Lists/Test%20docs/" -Path "D:\Exports\TestDoc"
Import-SPWeb -Identity http://spsdemo:6666 -Path "D:\Exports\TestDoc.cmp"

How to take backup and restore subsite and list using powershell

Example for Subsite backup and restore:

Export-SPWeb -Identity http://spsdemo:9999/Subsite1 -Path "D:\Exports\Subsite1"
Import-SPWeb http://spsdemo:6666/Subsite2 -Path "D:\Exports\Subsite1.cmp"

Example for list backup and restore:

Export-SPWeb http://spsdemo:9999/ -ItemUrl "/Lists/Test%20docs/" -Path "D:\Exports\TestDoc"
Import-SPWeb -Identity http://spsdemo:6666 -Path "D:\Exports\TestDoc.cmp"

Tip: Regarding SharePoint web part page.

By Adding query string parameters in the URL we can change mode of webpart page and also we can save time. Here we can see some query string and how web part page changes its mode.

How to put page in Webpart Maintenance mode

WebPart Maintenance page is an OOB page, which is used to manage the webparts in a webpart page. A webpart which is causing a problem in a webpart page can be removed using this page. Basically, a WebPart Maintenance page provides the list of all webparts added in a particular page and options to close or remove these webparts.
Sometimes after adding a webpart to the page, we get an error page with some message. But in this error page we wont get any SharePoint controls (like the SiteActions, Ribbon Menu etc.,), so we cannot edit the page and remove the webpart. Then we usually go to the edit form of the page (through page library) and then click on the 'Open Web Part Page in maintenance view' link, which opens the WebPart Maintenance page to remove the webpart which is causing the problem.

Instead of going to the Edit Properties form of the page, we can easily open the WebPart Maitenance page by just adding the following query string to the page URL


For example there is no “Edit Page” Option available in any of lists “NewFrom.aspx”. so there is no other way then to go in sharepoint designer and then edit the page.
But there is a shortcut using URL and you will have NewForm.aspx in Edit Mode in Browser.

The possible parameters with values are :

Add Web Parts/Browse ToolPaneView=2
Add Web Parts/Search ToolPaneView=3
Edit Mode                     mode=edit
View Mode                   mode=view
Shared Mode                PageView=Shared
Personal Mode              PageView=Personal

Sunday, February 9, 2014

How to run code Under administrator privileges in SharePoint

There are two ways to run a set of lines of code under administrator previlages.

Method 1:using runwithelivatedprevilages

this method runs under the Application Pool identity, which has site collection administrator privileges on all site collections hosted by that application pool.
NOTE: All the operations done inside this method should use a new SPSite Object. This means that the SPSite variables that are instantiated or created outside the subset code of this method can't be used for security elevation and it will result with the "Access denied error".


//SPSite Object created outside the RWEP Method
SPSite  site = new SPSite("siteURL");

    using (SPWeb web = site.OpenWeb())
        string user = web.CurrentUser.Name;
        //Operations that need high level access.



    //New SPSite object.
     using (SPSite site = new SPSite(web.Site.ID))
    //Do things by assuming the permission of the "system account".

Remember that, don’t use an object created outside of the RunWithElevatedPrivileges delegate within the delegate, and dispose all SPSite and SPWeb objects created within the delegate before the delegate completes.
And also dont use SPContext.Current.Web; to get SPWeb reference within the RunWithElevatedPrivileges.

Method 2: Using SPUserToken

Alternative impersonation technique to RunWithElevatedPrivileges() method:

We can either create a new SPSite instance from the existing SPSite Object, whose constructor will be provided by the "UserToken" of the System Account or we can assign the "UserToken" property of the SPSite object, see the below code snippet.

//Creating a SPUsertoken object.
SPUserToken saUserToken = SPContext.Current.Site.SystemAccount.UserToken;
//passing to the constructor of the SPSite

SPSite spSite=new SPSite("siteURL",saUserToken);


SPUserToken saUserToken = SPContext.Current.Site.SystemAccount.UserToken;
SPSite spSite=new SPSite("siteURL");
//assigning the UserToken property of SpSite.

spSite.UserToken= saUserToken;  

Bellow is example for UserToken

SPUserToken saUserToken = SPContext.Current.Site.SystemAccount.UserToken;

using (var site3 = new SPSite(site.OpenWeb().Url, saUserToken ))
// This code runs under the security context of the SHAREPOINT system
//  for all objects accessed through the "site" reference. Note that it’s a
// different reference than SPContext.Current.Site.

   using (var elevatedWeb = site3.OpenWeb())
       // Perform actions as SYSTEM here
       SPUser spUser = elevatedWeb.EnsureUser(username);
       SPGroup spGroup = elevatedWeb.SiteGroups["Partners"];
       if (spGroup != null)
          site3.AllowUnsafeUpdates = true;
          spGroup.AddUser(spUser.ToString(), spUser.Email.ToString(),                                           spUser.Name.ToString(), spUser.Notes.ToString());
          site3.AllowUnsafeUpdates = false;

Performance impact with RunWithElevatedPrivileges creates a new thread with the App Pool's credentials, blocking your current thread until it finishes.

Thursday, February 6, 2014

How to hide the "My Settings" and "Personalize this page" menu items.

i want to hide "my settings" and "personalize this page" items in welcome control for a particular group of users.


find group permission level and then

Using site setting:

Go to site settings --> Permissions --> Permission Levels --> Click on group's permission level and uncheck the bellow two check boxes under personal permissions.
1.  Manage Personal Views  -  Create, change, and delete personal views of lists.
2.  Update Personal Web Parts  -  Update Web Parts to display personalized information.

it might be difficult to go to every permission level and uncheck the above two options.
we can also hide  using jquery

Using JQuery:

<script type="text/javascript">
$("[text='My Settings']").remove();
$("[text='Personalize this Page']").remove();
$("[text='About Me']").remove();