Friday, July 6, 2012

enable developer dashboard sharepoint 2010

Usually when we are developing sharepoint application some times we found that page is taking lot of time to load the content.

In this kind of scinarios we need to figure out on which web part where it is taking long time to get the data to fix the performance issues.

Here sharepoipnt provided a good feature to calculate this kind of information called Developer dashboard.

steps to do this :

1. Enable developer dashboard through powershell

2. select developer dashboard and analyze your page health report.

Here is the Powershell script :

Dashboard : On

$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On
$addsetting.Update()


Dashboard : Off

$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off
$addsetting.Update()


After this you can just go to your site home page or any other site pages. Here you will find a new image icon just beside the login account name control like

















Click on that then you will get the complete report of the page like

1. how much time it is taking for each web part.

2. Stored procedures executing time.

3. Events load time etc..

















Thank you !!



sharepoint powershell missing shortcut

This is the one more issue when we work with sharepoint powershell.

Issue : When you run sharepoint powershell command for the first time. Some times you will get an error saying that "Missing shortcut".

Here is the screenshot of the same :

















Resolution :

1. Start -> Microsoft sharepoint 2010 products -> Sharepoint 2010 management shell.

2. Right click on this go to properties.

3. In Shortcuts tab change the Target value to %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit add-pssnapin microsoft.sharepoint.powershell

















<< This required admin control on the server >>

<< Adding this snap-in you can do manually by running windows powershell also >>

4. Save and close the wizard.

Now Open your Sharepoint powershell will works fine.

Thank you !!



Tuesday, July 3, 2012

property bag in sharepoint 2010

Sharepoint Property bags :

This is one of the good feature in sharepoint. This feature is already there in MOSS 2007 and it is carried to the latest version Sharepoint 2010 also.

This is basically used to store the key and value pair in hashtable format.

This is something like app.config information in classic ASP.NET.

Property bags can be created / Modified / Deleted from sharepoint designer or using Object model.

Using sharepoint designer :

1. Open a site in SharePoint Designer

2. Go to Site menu click on Site options

3. Site Settings dialog box opens

4. Click on Parameters tab where you can see the list of existing properties

from the same place you can even perform add/modify/delete operations.

















Using sharepoint object model :

using (SPSite RootSite = new SPSite(URL))
            {
                using (SPWeb web= RootSite.OpenWeb())
                {                    
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                       // Get Property bag
                        if (web.AllProperties.ContainsKey("SiteID"))
                        {
                            var data = web.AllProperties["SiteID"].ToString();
                        }                        
                        // Set Property bag
                        web.Properties["SiteID"] = "GUID";
                        web.Properties.Update();
                        web.AllowUnsafeUpdates = false;                        
                    }
                    catch (Exception ex) 
                    { 
                      //Throw Exception  
                    }           
                }
            }


NOTE : Property bags can be handled @Farm level, @Web application level, @Site level.

Thank you !!!



sharepoint powershell run timer job

In sharepoint deployment automation is the one of the key parts of development.

One of the key features of sharepoint is to create timer job which will execute a process at regular intervals of time.

Here comes the interesting part is to automate the timer job deployment.

There are 2 options for that.

1. Using Sharepoint object model which we will execute from the Feature activation.

2. Using powershell script which we can put as deployment files.

Here is the peace of code.

$WebApp = Get-SPWebApplication http://siteurl
$job = Get-SPTimerJob | ?{$_.Name -match $JobName} | ?{$_.Parent -eq $WebApp}



Thank you !!



Monday, July 2, 2012

sharepoint event handler redirect page

Here is the one more requirement to redirect user from event handler.

You can redirect user to a custom message page with code.

Here is the code for that.


/// 
       /// An item is being added.
       /// 
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            
            // Any condition 
            if (properties.ListItem["Manager"] != SPContext.Current.Web.CurrentUser.LoginName)
            {
            
                // Set the status property to CancelWithRedirectUrl this tells the event handler to cancel the operation
                properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                
                // Get the application page generic with SPUtility 
                properties.RedirectUrl = SPUtility.GetGenericSetupPath("/MyProj/MyPage.aspx");

            }
        }




Thank you !!!



sharepoint programmatically change master page

This article i will give you the peace of code where we can change the sharepoint master page programatically.

Here is the code snippet for the same.

 SPSecurity.RunWithElevatedPrivileges(delegate()
                   {
                       using (SPSite site = properties.Feature.Parent as SPSite)
                       {
                           using (SPWeb web = site.OpenWeb())
                           {
                               //Logging feature activation status in list
                               web.AllowUnsafeUpdates = true;

                               //Changing the master page
                               web.CustomMasterUrl = "/commerce/Custom/_catalogs/masterpage/Custom.master";
                               web.MasterUrl = "/commerce/Custom/_catalogs/masterpage/Custom.master";

                               //Setting the Navigation propertiers for web
                               web.AllProperties["__IncludeSubSitesInNavigation"] = "False";
                               web.AllProperties["__IncludePagesInNavigation"] = "True";

                               web.Update();
                            }
                         }
                      }     


web.AllProperties methods will change the navigation source for the web application, This you can change according to business need.

Thank you !!



sharepoint designer access denied

Here is an interesting error you will get some times while working with Sharepoint designer i.e "Access denied"

Solution :

If you get this error make sure the attempted login user is a site collection if not add as site collection administrator.

Hope this will help !!!