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 am looking for a way to run PowerShell from C# code. What is more, I need the code to activate the following features in my Sharepoint site that gets created:

1) SharePoint Server Publishing Infrastructure

and

2) Sharepoint Server Publishing

How would I do this for PowerShell code in C#?

share|improve this question
    
I'm curious, why PowerShell through C# and not just activate from C# code? Also, which version of SharePoint are you using? –  Louis Apr 4 '12 at 21:54
    
Yeah, it does seem unnecessary. Unless you're trying to run a cmdlet who's function isn't exposed in the SharePoint object model. –  RJ Cuthbertson Apr 5 '12 at 0:59
add comment

2 Answers

Check this out to activate features in C#:

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/22/how-to-programmatically-activate-a-feature.aspx

And a list of SharePoint OOTB feature guids:

http://blogs.msdn.com/b/mcsnoiwb/archive/2010/01/07/features-and-their-guid-s-in-sp2010.aspx

================================================================

If you really want to run PowerShell from C#:

http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

Don't forget to add the SharePoint snapin to expose the cmdlets:

Add-PSSnapin Microsoft.SharePoint.PowerShell
share|improve this answer
add comment

PowerShell & C# are two languages you can use to access the SharePoint Object Model. Depending upon the kind of task you want to perform on SharePoint platform, you can choose any one. In general, almost every task is achievable using C# or Powershell

Using C#, If you want to activate a feature when a new site is created, below is the code you can use in WebProvisioned Event Receiver.

Guid featureGuid = new Guid(YourFeatureGUID);
if (web.Features[featureGuid] == null) 
{
         web.Features.Add(featureGuid);
}

By the way, I would recommend to create the site using Publishing Site definition so that you get all publishing features activated by default.

share|improve this answer
add comment

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.