SharePoint: How To Create a SharePoint Permission Group and Add Users Programmatically

Using C# and SharePoint Object Model I will show you how to add a user to an existing or newly created SharePoint Permission Group.

I was working on a project that requires a domain user to be added to the site’s visitors group automatically base on events, and my project is developed in Microsoft Visual Studio so I need write some code to achieve this.

Below is an example code snippet that will show you how to to use SiteGroups.Add() to create a new SharePoint Group and assign the Read permission level by using RoleDefinitionBindings.Add(), add a default user and a group owner:

using (var spSite = new SPSite("SiteURL"))
{
	using (var web = spSite.OpenWeb())
	{
		var groupName = "Group Name";
		var groupDescription = "Group Description";
		var groupOwner = "contoso\\\jbloggs";
		var defaultUser = "contoso\\\jdoe";
		
		
		web.SiteGroups.Add(groupName, groupOwner, defaultUser, groupDescription);
		
		var group = web.SiteGroups[groupName];
		var spRoleAssignment = new SPRoleAssignment(group);
		var permissionName = "Read";
		
		spRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions[permissionName]);
		web.RoleAssignments.Add(spRoleAssignment);

		web.Update();
	}
}

(more…)

SharePoint 2013: Retrieving User’s Profile using Client Side Object Model

SharePoint-logo-1024x325

Using the SharePoint Client Side Object Model you can retrieve a specific user’s profile from SharePoint. I have put together a simple console application in Visual Studio and I have described each line of code below.

On the 1st line of code instantiate the ClientContext class by passing your site’s URL.

On the 2nd line I instantiate PeopleManager class which provides methods for operations on people in SharePoint.

On 3rd line I instantiate the UserProfilePropertiesForUser class by passing the client context, the user’s name and an array of strings containing the property names.

On the 4th line I use the GetUserProfilePropertiesFor method from the PeopleManager and passing it the UserProfilePropertiesForUser object I have created earlier on. This method will return a list of string values that we can use a foreach loop to iterate through the values.

var clientContext = new ClientContext("http://yoursite/");

var peopleManager = new PeopleManager(clientContext);

var userProfile = new UserProfilePropertiesForUser(clientContext, "domain\\username", new[] {"FirstName", "AboutMe", "Manager"});

var profileProperties = peopleManager.GetUserProfilePropertiesFor(userProfile);

clientContext.ExecuteQuery();

foreach (var str in profileProperties)
{
	Console.WriteLine(str);
}

Console.ReadKey();

C#: How to get your computer and user name.

If you want to get your computer and user name from Windows in C# try this by using the WindowsIdentity class from System.Security.Principal namespace:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.Out.WriteLine(WindowsIdentity.GetCurrent().Name);
        }
    }
}
Shares