Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Thursday, November 8, 2012

Developer Dashboard in SP2010 and new 2013


Have you ever told to figure out why your SharePoint page is consuming much time? In projects where the page render SLAs are at an aggressive end - like one of my current project - we SharePoint engineers will have a real good time...

In the older versions (MOSS 2007, SPS 2003) this was too tuff - personally I used to follow a manual approach in isolating the issue by removing web parts one by one etc... I believe SharePoint product team invested a lot and came up real good feature in SP2010 named Developer Dashboard.

Developer Dashboard is designed to provide additional performance and tracing information that can be used to debug and troubleshoot issues with page rendering time.  This used often by the administrators whenever there is a need to identify / improve page performance. Enabling this great feature will help you get critical information about execution time, log correlation ID, critical events, database queries, service calls, SPRequests allocation and web part events offsets etc.

We can set the DashBoard in the following 3 levels
OnDemand. : In this mode you should see on the left hand side of the ribbon a small icon next to the login credentials. On clicking that icon - SharePoint will toggle the dashboard display beneath the page output. This toggle option will be available only to site collection admins. Looks promising right...
On. :  In this mode you will not see the small icon at the top of the page but the Developer Dashboard will be available on the bottom of your entire page always.
Off. : In this mode you neither see the dashboard nor the icon to display it.

By default the Developer Dashboard will be turned off - it can be enabled easily using object model code, STSADM commands or PowerShell script.

The following script/command will enable developer dashboard in "OnDemand" mode. To switch it on and of , use "on" and "off" parameter values respectively.
PowerShell
$sp =[Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$sp.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand;
$sp.RequiredPermissions = 'EmptyMask';
$sp.TraceEnabled = $true;
$sp.Update();

STSADM
stsadm -o setproperty -pn developer-dashboard -pv ondemand

Object Model
SPWebService sp = SPWebService.ContentService;
sp.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.OnDemand;
sp.DeveloperDashboardSettings.Update();

Note: DeveloperDashboardSettings has a property called RequiredPermissions.  You can assign a collection of base permissions (like EditLists, CreateGroups, ManageAlerts, or whatever you want) to it; only those people that have those permissions will be able to see the output.
Note: Developer Dashboard is a farm level setting. So If you code the above object model snippet  up in a web part and try to execute it in a non-central admin site, it will throw a security exception.
Note : code from Sandboxed component won’t be showing up in Dashboard since its running in completely different process from the page request.

What is there in Dashboard
In the Dashboard you will find information about the controls, queries and execution time that occur as part of the page rendering process. Usually it provides information from the perspective of the event pipeline, the web server and database. On the left side you can see the different events that fired in the page processing pipeline and its processing duration.  On the top right hand side you see information about the page processing as whole, including the overall execution time, the amount of memory used in the processing of the page request and the correlation ID. Correlation IDs are great info when trying to link the page render to entries in the ULS log.  Underneath the server information you will find a list of the different database calls that were made through the object model by various components in the page itself as well as the controls it hosts. There is an additional option to to see the call stack of every database call - by clicking the database call hyperlinks.

Why to use SPMonitorScope as a best practice
Developer DashBoard does not display the entire set of events instead the code you have in your override for OnInit or Render will be captured in this pipeline.  To ensure the trace of code in other places use the new object model class called the SPMonitoredScope it helps to keep track of useful usage and tracing information just like the developer dashboard uses. You may use it as shown below

using (SPMonitoredScope scpBtnGetScore = new SPMonitoredScope("BtnGetScore_Click"))
{//add your custom code here}
In my understanding this is the only option to understand an manage the custom components once you deploy it in PROD.

Whats new in SP2013
Developer Dashboard in SP2013 is using dedicated WCF service (diagnosticsdata.svc) it allows Detailed request information per page with chant view and additional detailed information included for request analyzing. There is an Interactive addition in the new Developer Dashboard with which you can now View ULS logs (under a dedicated tab) for particular request. This will definitely save developer since it avoids the dependency with admin teams to get ULS logs that they need. Now Developer Dashboard is running in separate window to avoid affecting rendering of actual page.

Don’t know why the OnDemand option is deprecated: Only ON & OFF settings are available. Will share more details soon...

Thursday, August 23, 2012

Right SharePoint 2013 APIs for your needs – A ready reckoner


SharePoint products team amazes me every time they introduce a product version release. There are several options available when it comes to access SharePoint 2013 using APIs. Per my understanding the following will be the factors that decide which option to use
  • Type of customization like web parts, Silverlight App, SharePoint App Page, timer job and more. All these customization are different in most aspects and choosing right API option would eventually decide its eventual success
  • Enterprise governance rules will be the key differentiator in deciding the API option.
  • Development skill level is the other factor. Expertize in a the technologies like .NET Framework, Silverlight, PowerShell, JavaScript, ASP.NET, REST/OData will ensure the best development practices are applied in design / development of custom components
  • Devices we are targeting like farm server, client computer, mobile devices etc. The customization targeting different device should use appropriate API option.


Let me share a high level mapping based on the current info ...

API Option
Custom Component
Server Object Model
PowerShell script for farm / web app maintenance, tracking etc
Timer job to perform scheduled actions on web app
Web Parts to be displayed in sites
Silverlight client object model
Web Parts to be displayed in sites
SharePoint  App
Silverlight Application that integrate with SharePoint site data
ASP.Net web application that integrate SharePoint site data
REST/Odata Endpoints
ASP.Net web application that integrate SharePoint site data
SharePoint  App
HTML/Javascript application that consume SharePoint site data
App for office
LAMP web application that integrate SharePoint site data
iOS/Android app for mobile devices that access SharePoint site data
Javascript APIs
Web Parts to be displayed in sites
ASP.Net web application that integrate SharePoint site data
SharePoint  App
App for office
HTML/Javascript application that consume SharePoint site data
.Net Client Object Model
.Net application that consume SharePoint site data
Mobile Silverlight API
Silverlight app for windows phone
XNA app for Windows Phone


more to follow... 

Sunday, July 29, 2012

Update created by column using powershell

Was busy in implementing a test site - my BA asked whether we can show the CEO's name instead of her name in the created by column of all items in a list.

$SPSite = New-Object Microsoft.SharePoint.SPSite("<site collection url>")
$SPWeb = $SPSite.AllWebs["<site name>"] 
$SPList = $SPWeb.Lists["<List Name>"] 
$SPListItemCollection = $SPList.Items 
$targetUser =$SPWeb.EnsureUser(“<user Windows ID>”)
foreach ($ListItem in $SPListItemCollection) 
    {   
        $ListItem["Author"] = $targetUser 
$ListItem.ModerationInformation.Status = "Approved"
        $ListItem.Update() 
    }
$SPWeb.Update()


Worked like a gem .... BA happy, CEO tooo :)

Sunday, April 8, 2012

Monday, February 27, 2012

Powershell alternative of stsadm -o migrateuser

Powershell alternative of stsadm -o migrateuser -oldlogin domain\oldid  -newlogin domain\newid  -ignoresidhistory

I found the following with the help of MSDN techies

Move-SPUser [-Identity] <SPUserPipeBind> -NewAlias <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-IgnoreSID <SwitchParameter>] [-WhatIf [<SwitchParameter>]]

Saturday, January 7, 2012

Quick way to dump UPS into a CSV file

Have you ever asked to get a CSV dump of user profiles from your SharePoint User Profile Service application....?


lets try this Powershell script 


$outfile = "c:\UserProfiles.csv"; 
$arProperties ="Accountname","FirstName","LastName","Workphone","Department","Title"; 
$mySiteUrl = "https://mysite.nilaa.com/"  
$site = new-object Microsoft.SharePoint.SPSite($mySiteUrl);  
$serviceContext = Get-SPServiceContext $site 
$upm = New-Object  Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)

$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)  
$AllProfiles = $ProfileManager.GetEnumerator()


$header = [string]::join("`t",$arProperties); 
$header | Out-File -FilePath $outfile -Encoding Unicode


$AllProfiles | ForEach-Object {  
  foreach($p in $arProperties){ $arProfileProps += $_.Item($p) + "`t"; } 
  $results = [string]::join($delim,$arProfileProps); 
  $CleanResults = $results.Replace("`n",''); 
  $CleanResults = $CleanResults.Replace(","," ");    
$CleanResults | Out-File -Append -FilePath $outfile -Encoding Unicode 
Remove-Variable -Name arProfileProps; 
    }

$site.Dispose()

Saturday, September 24, 2011

To remove button from Ribbon

Following are the steps to remove this button from Ribbon


In Visual Studio 2010:
  1. Create an empty SharePoint 2010 Solution , you may give your favorite name to it
  2. Add a feature by right clicking on the Features in Solution Explorer
  3. Right click on the Project and add a new item, choose empty element and name it
  4. Open the Elements.xml file from the newly added item
  5. Copy & paste the following XML into the Elements.xml file
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
     Id="RemoveRibbonButton"
     Location="CommandUI.Ribbon">
       <CommandUIExtension>
           <CommandUIDefinitions>
              <CommandUIDefinition
               Location="Ribbon.Library.Actions.OpenWithExplorer" />
           </CommandUIDefinitions>
       </CommandUIExtension>
    </CustomAction>
</Elements>

Deploy the Solution and observe that “Open with Explorer” button will be removed from the Ribbon.


Without Visual Studio: With SharePoint 2010 Management Shell (PowerShell)

  • Create a folder under %Program Files%\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\RemoveRibbonButton
  • Create a feature.xml file in RemoveRibbonButton.

<?xml version="1.0" encoding="utf-8" ?>

 <Feature Id="MyFeatureId"
 Title="Remove button feature"
 Description="Description of the Feature." 
 Version="1.0.0.0" 
 Scope="Web" 
 xmlns="http://schemas.microsoft.com/sharepoint/">
 <ElementManifests>
  <ElementManifest Location="manifest.xml" />   
  </ElementManifests>
 </Feature>


  • Create a blank xml file - manifest.xml in RemoveRibbonButton folder and add the below content to it and Save it.


<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
     Id="RemoveRibbonButton"
     Location="CommandUI.Ribbon">
       <CommandUIExtension>
           <CommandUIDefinitions>
              <CommandUIDefinition
Location="Ribbon.Library.Actions.OpenWithExplorer" />
           </CommandUIDefinitions>
       </CommandUIExtension>
    </CustomAction>
</Elements>

  • Deploy the customization using SharePoint 2010 Management Shell (or Windows PowerShell): Install-SPFeature MyFeatureId Enable-SPFeature FeatureId -Url http://server/site/subsite (replace http://server/site/subsite on your site url)