.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Adding user in sharepoint site using powershell

Posted By:Sunil Yadav       Posted Date: June 30, 2010    Points: 5    Category: ASP.NET    URL: http://www.dotnetspark.com  

This articles shows how we can add users in sharepoint using powershell.

What is Windows Powershell ?

Windows PowerShell command-line interface is a new command-line tool and supporting scripting language from Microsoft that complements Cmd.exe in the Windows administration context. In the SharePoint administration context, Windows PowerShell supersedes the Stsadm.exe administration tool. Moving forward, you should use Windows PowerShell scripting technology to develop any new command-line scripts in SharePoint Foundation 2010. Firstly we need to load sharepoit assembly in powershell console which is the reference to all SharePoint objects.
PS C:\Users\sunil> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")

GAC Version Location
--- ------- --------
True v2.0.50727 C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint\\Microsoft.SharePo...
Create a site object to hold the SPSite reference.
PS C:\Users\sunil> $site=new-object Microsoft.Sharepoint.SPSite("http://mossportal")
obtain an reference to the Web site that is associated with the URL that is used in an SPSite constructor.
PS C:\Users\sunil> $web=$site.OpenWeb()
Now defines the role assignments for a user.
PS C:\Users\sunil> $spRoleAssignment=new-object Microsoft.SharePoint.SPRoleAssignment("mossportal\sunil", "sunilyadav165@gmail.com", "Sunil Yadav", "Sharepoint Developer")
Get the role from the web and set the base permission for a role definition.
>> $spRoleDefinition=$web.RoleDefinitions["Read"]
>> $spRoleAssignment.RoleDefinitionBindings.Add($spRoleDefinition)
Finally add the RoleAssignments to the current we and update the spweb object.
>> $web.RoleAssignments.Add($spRoleAssignment)
>> $web.Update()
Consider below image which depicts the above.

Reference: http://msdn.microsoft.com/en-us/library/ee539977.aspx
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend