Friday, December 13, 2013

How to Add User to Default SharePoint Groups Programatically

As every SharePoint developer or user know that when a new Site is created, Obviously 3 commonly used site specific groups will be created automatically along with some general groups.
<Site Name> Owners
<Site Name> Members
<Site Name> Visitors

To add an user to any of the above three groups of a site, we have a API method to get the groups associated with that site, programatically.
using (SPSite site = new SPSite("http://siteurl"))
    using (SPWeb web = site.RootWeb)
        ////Get the site default groups
        SPGroup owernsGroup = web.AssociatedOwnerGroup;
        SPGroup membersGroup = web.AssociatedMemberGroup;
        SPGroup visitorsGroup = web.AssociatedVisitorGroup;

        ////Add user to groups
        SPUser user = web.EnsureUser("login");
        if (null != user)

Also we can add user to other default groups like Approvers, Designers and Viewers etc., But there is no API property for web to get these groups. So we can get the group by Name.
if (web.GroupExists("Approvers"))
    ////Get approvers group
    SPGroup approversGroup = web.Groups["Approvers"];            
    if (null != approversGroup)
        ////Add user to group
        SPUser user = web.EnsureUser("login");

Check User exists in SharePoint Group

I was looking for direct API method to check whether specified user exists in SharePoint group. Unfortunately there is no direct API check.

I found below single statement is efficient for this check. I made it a generic for re-usability.

SPGroup spGroup = web.Groups["Group Name"];
if (null != spGroup)
  SPUser spUser = web.EnsureUser("Login Account");
  if (null != spUser)
    bool userExsists = spUser.Groups.Cast<SPGroup>().Any(g => g.Name.ToLower() == spGroup.Name.ToLower());
     if (!userExsists)

Monday, January 28, 2013

Load SharePoint PowerShell Commandlets in the PowerShell ISE

We know that Windows PowerShell ISE is a nice editor for writing and executing PowerShell scripts. But for SharePoint Administrators/Developers none of the SharePoint PowerShell Command-lets are loaded by default, so need to Add the Snap-In explicitly. But I wanted to load the SharePoint snap-in to ISE one-time and use it further without loading again-and-again.

Execute the below PowerShell script in ISE to load the snap-in to ISE profile.
if (!(test-path $profile.AllUsersAllHosts)) 
   new-item -type file -path $profile.AllUsersAllHosts-force

$cmd = 'if((Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) { Add-PSSnapIn "Microsoft.SharePoint.Powershell" }'

out-file -FilePath $profile -InputObject $cmd -Append
Now close and open the ISE, It will load SharePoint snap-in automatically. Intellisense is available for all the SharePoint Command-lets.

Alternatively a single line of statement can be added to ISE to load the SharePoint SnapIn
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
Hope this saves your time... 

Enable PowerShell ISE Editor

Good news for all the PowerShell developers/users. I know writing or editing a PowerShell script in Notepad or Visual Studio is not at all user-friendly and annoying. To overcome all such problems we can enable PowerShell ISE which provides a nice interface with script and output windows side by side.

Windows Server 2008 R2:
Execute the below commands on PowerShell command prompt.
1. Import Module Server Manager
    Import-Module ServerManager

2. Now search for PowerShell ISE feature in Windows Features
   Get-WindowsFeature -Name *PowerShell*

3. Verify PowerShell-ISE feature exists in previous result and add the feature
   Add-WindowsFeature PowerShell-ISE

Now the PowerShell ISE enabled on the server. To open the editor check for the Windows PowerShell ISE shortcut in start menu or open the PoserShell command prompt and execute the command ise.

Windows Server 2012:
PowerShell ISE comes by default.

Hope this helps making PowerShell scripts creation and execution easy and simple.