Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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();
share|improve this answer
    
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. –  Tim Gabrhel Jun 10 '11 at 21:14
1  
Access denied to the whole page? Have you tried runwithelevatedprivileges msdn.microsoft.com/en-us/library/…? –  Brage Tukkensæter Jun 10 '11 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/… –  Tim Gabrhel Jun 10 '11 at 21:34
    
I will have to change the scope of my feature for the managed code to work –  Tim Gabrhel Jun 10 '11 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) –  Neil Richards Jun 11 '11 at 18:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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