Question

My web application is an ASP.NET MVC 4 web application using HTML5 and Jquery.

I'm writing a web application to retrieve and edit data from a Sharepoint server using Active Directory.

I'm able to retrieve information fine, but I'm trying to figure out a way to edit and commit changes to an active directory account. I haven't been able to find code samples anywhere that pertain to remote web application access. The only samples of editing I've seen can only be done on the SharePoint server.

I'm wondering if we're missing something entirely about Active Directory and if what I'm trying to do is even possible.

NOTE:

I haven't been able to find code for editing Active Directory information as of yet. This is the code I have so far for retrieval. I want to be able to pull back information, edit a property such as First Name or Last Name, and then commit the changes to SharePoint Active Directory.

Thanks in advance for your answers!

ClientContext currentContext = new ClientContext(serverAddress);
        currentContext.Credentials = new System.Net.NetworkCredential(adminAccount, password);

        const string targetUser = "domain\\targetAccountName";

        Microsoft.SharePoint.Client.UserProfiles.PeopleManager peopleManager = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(currentContext);
        Microsoft.SharePoint.Client.UserProfiles.PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);

        currentContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
        currentContext.ExecuteQuery();

        foreach (var property in personProperties.UserProfileProperties)
        {
            //Pull User Account Name
            //Edit Account name to new value
            //Commit changes
        }            
Was it helpful?

Solution

Looks like the PersonProperties class only provides readonly access as all the properties only show Get. MSDN PersonProperties

If you want to stay within SharePoint, it looks like you'll need to check out the UserProfile class. That page has a decent example of retrieving an account and then setting some properties.

If you don't need SharePoint specific properties and want an easy to use format you could retrieve the UserPrincipal. It will give you easy access to common user properties.

using (var context = new PrincipalContext(ContextType.Domain, "domainServer", 
                            "DC=domain,DC=local", adminAccount, password))
{
    var userPrincipal = UserPrincipal.FindByIdentity(context, 
                            IdentityType.SamAccountName, "targetAccountName");
    if (userPrincipal != null)
    {
        userPrincipal.GivenName = "NewFirstName";
        // etc, etc.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top