0

I have a feature that deploys an application page to the layouts directory. I am attempting to update the content (or config?) database by executing a powershell command from c#.

The following code will execute fine and give me the current status of the developer dashboard (on,off,ondemand).

PowerShell ps = PowerShell.Create();
ps.AddScript("Add-PsSnapin Microsoft.SharePoint.Powershell");
ps.AddScript("$svc=[Microsoft.SharePoint.Administration.SPWebService]::ContentService");
ps.AddScript("$ddsetting=$svc.DeveloperDashboardSettings");
ps.AddScript("$ddsetting.DisplayLevel");
System.Collections.ObjectModel.Collection<string> output = ps.Invoke<string>();

I am attempting to update this setting.

PowerShell ps = PowerShell.Create();
ps.AddScript("Add-PsSnapin Microsoft.SharePoint.Powershell");
ps.AddScript("$svc=[Microsoft.SharePoint.Administration.SPWebService]::ContentService");
ps.AddScript("$ddsetting=$svc.DeveloperDashboardSettings");
ps.AddScript(string.Format("$ddsetting.DisplayLevel=[Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::{0}", rblDashboard.SelectedValue));
ps.AddScript("$ddsetting.Update()");
ps.Invoke<string>();

I can step through the code just fine, but the setting is never updated. The string.format line evaluates as I'd expect. That value is changed like it should.

1 Answer 1

3

I guess there will be ok to run Powershell from SharePoint, but why will you not use managed code for this? Powershell is great for scripting, but most of the commands can also be found in managed code.

The Microsoft.SharePoint.Administration.SPDeveloperDashboardSettings class can help.

SPDeveloperDashboardSettings settings =
SPWebService.ContentService.DeveloperDashboardSettings;
settings.DisplayLevel = SPDeveloperDashboardLevel.On;
settings.TraceEnabled = true;
settings.Update();
5
  • I up voted you because I didn't know this existed. This appears that it is trying to updated the Config database, which I am not going to be able to do from an application page. I get an access denied message. I believe that's why I was lead down the powershell route. Commented Jun 10, 2011 at 21:14
  • 1
    Access denied to the whole page? Have you tried runwithelevatedprivileges msdn.microsoft.com/en-us/library/…? Commented Jun 10, 2011 at 21:31
  • Yes. Runwithevelevated priviledges wouldn't help here. I just remembered I had this other post that contained the details: sharepoint.stackexchange.com/questions/13463/… Commented Jun 10, 2011 at 21:34
  • I will have to change the scope of my feature for the managed code to work Commented Jun 10, 2011 at 21:34
  • 1
    Ultimately you're trying to do something that is actually not authorised (i.e. activating a Farm level feature using an account which only has access to the Web-App). I would probably break out the developer dashboard bit into a separate feature so that the different bits of you solution are scoped at the appropriate level (or use Wouter's feature that he's already developed >> blogs.code-counsel.net/Wouter/Lists/Posts/Post.aspx?ID=130) Commented Jun 11, 2011 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.