Skip to content

@JaredMeredith

Learn. Share. Repeat.

Contact

  • Blog
  • About Me
  • Strengths Finder Top 5
    • Strengths Finder All 34
  • 16Peronalities
  • Builder Profile 10
  • Programmatically update the manager field from a SQL table for sharepoint user profile

    05/29/2015

    +

    +

    +

    +

    +

    +

    Oh, what a journey this was. Googling, reading, BCS this, BCS that, you can do it, you can’t do it, then a somewhat vague article that almost explained it (but not quite of course). As you may or may not know, updating the manager field in user profile is a pain. You cannot map it to anything other than BCS through the built-in mechanisms. This is because of the complicated person type in SharePoint (which by default is mostly not to be messed with).

    After much travail I came out the other side of the tunnel with a working solution to update the user profile manager field programmatically from SQL. This snippet loops through my SQL table, checks for the user in user profile. If the user exists then it will check to ensure the manager exists. If both check out then the update statement executes. The results are gathered as a log and emailed as html.

    I created a SQL table that had the user to update and the manager to update for that user. I placed the SQL table on the same server as my sharepoint db so I could utilize the accounts that were already created there.

    I ended up putting my code snippet inside of the Execute event on a custom SharePoint timer job. For info on creating custom timer jobs, you can consult this article.

    Here’s my using statements above the namespace on the timer job:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Net.Mail;
    using System.Text;
    using System.Web;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.Office.Server;
    using Microsoft.Office.Server.UserProfiles;
    

    Once you have a timer job just insert this snippet into your execute event.

    string message = "";
    
    int counter = 0;
    
    // get a reference to the current site collection's content database
    SPWebApplication webApplication = this.Parent as SPWebApplication;
    SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
    
    // proceed if you have a valid content database id
    using (SPSite site = new SPSite(contentDb.Sites[0].ID))
    {
    // proceed if you have a valid site service context based on the site
    using (new SPServiceContextScope(SPServiceContext.GetContext(site)))
    {
    // create a reusable user profile user container
    UserProfileManager upm = new UserProfileManager();
    
    // sql string
    using (var connection = new SqlConnection("Data Source=SERVERNAME;Initial Catalog=YOUR_DB;User Id=DOMAIN\\userWithSQLAccess;Password=userWithSQLAccessPwd;Integrated Security=SSPI;"))
    {
    // perform the following transactions with the following SQL command
    using (var command = connection.CreateCommand())
    {
    // query used in the database that populates the users whom we want to update their manager with
    command.CommandText = "SELECT [AccountName], [ManagerAcctName] FROM [YOUR_DB].[dbo].[YOUR_TABLE]";
    
    // open the connection and start the reader
    connection.Open();
    using (var reader = command.ExecuteReader())
    {
    // retrieve and assign values from SQL to object variables
    var indexOfColumn1 = reader.GetOrdinal("AccountName");
    var indexOfColumn2 = reader.GetOrdinal("ManagerAcctName");
    
    // perform while there are row results from the query above
    while (reader.Read())
    {
    // assign reader object values to object variables
    var value1 = reader.GetValue(indexOfColumn1);
    var value2 = reader.GetValue(indexOfColumn2);
    
    // create strings and assigned converted string values of object variables
    string sAccount = String.Empty;
    string sManager = String.Empty;
    string sManagerAcctName = String.Empty;
    string oldManager = String.Empty;
    sAccount = value1.ToString();
    sManagerAcctName = value2.ToString();
    
    // check to see that the user profile exists for an account
    if (upm.UserExists(sAccount))
    {
    // create a reusable user profile for checking SQL per row
    UserProfile u = upm.GetUserProfile(sAccount);
    
    // check for the presense of an old manager
    if (String.IsNullOrEmpty(u["Manager"].Value.ToString()))
    {
    // if the manager doesn't exist clear the value
    oldManager = "";
    }
    else
    {
    // if the manager exists the place it there to log it
    oldManager = u["Manager"].Value.ToString();
    }
    
    // verify that the manager exists
    if (sManager != null)
    {
    // create and assign the string value of the manager value from SQL
    string mAccount = sManagerAcctName.ToString();
    
    // verify that there is a user profile value for the manager field given from SQL
    if (upm.UserExists(mAccount))
    {
    // Assign the manager value from SQL to the user profile manager field
    u["Manager"].Value = mAccount;
    // commit the user profile save
    u.Commit();
    
    // check to see if there was an old manager field populated
    if (string.IsNullOrEmpty(oldManager))
    {
    // log that user had no prior manager but will now after this update
    message = message + sAccount + " had no previous manager but is now updated " + " to " + mAccount + "<br />";
    }
    else
    {
    // log that the user had a prior manager but is now updated to a new one from this process
    message = message + sAccount + " manager was updated from " + oldManager + " to " + mAccount + "<br />";
    }
    
    // keep count
    counter = counter + 1;
    }
    else
    {
    // if you are here than the manager value from SQL is not a valid user profile user = SKIP
    }
    }
    else
    {
    // if you are here then there is not a manager value in the SQL data = SKIP
    }
    }
    else
    {
    // if you are here than the row on the SQL data was not a valid user profile user = SKIP
    }
    }
    }
    
    // close the SQL connection when done
    connection.Close();
    
    // attempt to send the email message
    try
    {
    // definet the mail client that will send the message
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.Host = "exchangeserver.yourdomain.com";
    
    // get and assign the to field of the email message
    string toemail = "";
    toemail = "yoursharepointadmin@yourdomain.com";
    
    // build the mail message for the mail client to send
    MailMessage mm = new MailMessage();
    mm.From = new MailAddress("donotreply@yourdomain.com");
    mm.To.Add(new MailAddress(toemail));
    
    // put the rest of email together
    mm.Subject = "AD Manager To UPS Timer Job - " + counter.ToString() + " records processed";
    mm.Body = message;
    mm.IsBodyHtml = true;
    
    // send the message
    client.Send(mm);
    }
    catch
    {
    // handle error if you want
    }
    }
    }
    }
    }
    

    Help me out and share!

    • Email a link to a friend (Opens in new window) Email
    • Print (Opens in new window) Print
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on Facebook (Opens in new window) Facebook
    • Share on X (Opens in new window) X
    • Share on Reddit (Opens in new window) Reddit
    • Share on Tumblr (Opens in new window) Tumblr
    • More
    • Share on Pinterest (Opens in new window) Pinterest
    • Share on Telegram (Opens in new window) Telegram
    • Share on WhatsApp (Opens in new window) WhatsApp
    Like Loading…

    +

    +

    +

    +

    +

    +

    + C#, Email, Programming, SharePoint, SQL
    + C#, Email, SharePoint, SQL, user profile

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

  • Programmatically Send An Email Message With An Html Body (C#)

    05/28/2015

    +

    +

    +

    +

    +

    +

    I have used the following snippet many times in the past. I would recommend validating your string values if the values are dynamic. I would also recommend placing your email attempt in a try/catch so you will not break your web application if there is some kind of issue on the email server itself. Anyway, hope this helps.

    using System.Net.Mail;
    
    // server settings
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.Host = "exchangeserver.yourdomain.com";
    
    // declasre mail message
    MailMessage mm = new MailMessage();
    
    // from
    mm.From = new MailAddress("donotreply@yourdomain.com");
    
    // to
    string toemail = "";
    toemail = "persontosendto@yourdomain.com";
    mm.To.Add(new MailAddress(toemail));
    
    // cc
    string toemailcc = "";
    toemailcc = "another_person@yourdomain.com";
    mm.CC.Add(new MailAddress(toemailcc));
    
    // subject
    mm.Subject = "The All Important Subject";
    
    // body
    string message = "";
    message = "This is the all important email body message. <br /><br />";
    message = message + "Sincerely yours, <br /><br />";
    mm.Body = message;
    mm.IsBodyHtml = true;
    
    // send the message
    client.Send(mm);
    

    Help me out and share!

    • Email a link to a friend (Opens in new window) Email
    • Print (Opens in new window) Print
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on Facebook (Opens in new window) Facebook
    • Share on X (Opens in new window) X
    • Share on Reddit (Opens in new window) Reddit
    • Share on Tumblr (Opens in new window) Tumblr
    • More
    • Share on Pinterest (Opens in new window) Pinterest
    • Share on Telegram (Opens in new window) Telegram
    • Share on WhatsApp (Opens in new window) WhatsApp
    Like Loading…

    +

    +

    +

    +

    +

    +

    + C#, Email, Programming, Web
    + C#, code snippet, Email

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

  • Check For A Referring Page URL And Redirect (Classic ASP)

    05/22/2015

    +

    +

    +

    +

    +

    +

    I spent a while looking around for something that would help me tighten up some pages that did not need reached unless they came from another page that I had authorized.

    In this example we check first that there is a referring URL and if there is not then go to my error page. The next part two checks look to ensure that the referring page is not the two pages we are okay with the referring URL to be.  If the referring URL is not either of those then we know the previous page is not what we want either and we refer to an error page.

    <%
    referer=request.servervariables("http_referer")
    if (referer = "") then
    Response.Redirect "error.asp"
    elseif (referer <> "<a href="https://www.google.com">https://www.google.com</a>") then
    
    elseif (referer <> "<a href="https://www.yahoo.com">https://www.yahoo.com</a>") then
    
    else
    Response.Redirect "error.asp"
    end if
    %>
    

    Help me out and share!

    • Email a link to a friend (Opens in new window) Email
    • Print (Opens in new window) Print
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on Facebook (Opens in new window) Facebook
    • Share on X (Opens in new window) X
    • Share on Reddit (Opens in new window) Reddit
    • Share on Tumblr (Opens in new window) Tumblr
    • More
    • Share on Pinterest (Opens in new window) Pinterest
    • Share on Telegram (Opens in new window) Telegram
    • Share on WhatsApp (Opens in new window) WhatsApp
    Like Loading…

    +

    +

    +

    +

    +

    +

    + Classic ASP, Web
    + classic asp, redirect, referrer url

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

  • Unlock Or SetPassword For An Active Directory Account Within A Web Application (VB)

    05/22/2015

    +

    +

    +

    +

    +

    +

    Wrote a variation of this for different purposes over the past few months. You can get creative and put it in a web service or within your web application. You would also want to send a lot of the initial variables in through ByVal when you call the Function or Method.

    Dim user As String = "yourdomain/userToUnlock"
    Dim pwd As String = "userToUnlockPW"
    Dim de As DirectoryEntry = Nothing
    Dim ds As DirectorySearcher = Nothing
    Dim url As String = "LDAP://111.11.111.1111/dc=yourdomain,dc=com"
    de = New DirectoryEntry(url)
    de.Username = "yourdomain/authorizedADuser"
    de.Password = "authorizedADuserPW"
    de.AuthenticationType = AuthenticationTypes.Secure Or AuthenticationTypes.ServerBind Or AuthenticationTypes.Sealing
    ds = New DirectorySearcher(de)
    ds.SearchRoot = de
    ds.Filter = String.Format("(&amp;(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", user)
    ds.PropertiesToLoad.Add("userAccountControl")
    Dim result As SearchResult = ds.FindOne
    If Not result Is Nothing Then
    de = result.GetDirectoryEntry
    de.RefreshCache()
    de.Properties("LockOutTime").Value = 0
    de.Properties("userAccountControl").Value = &amp;H200
    de.Invoke("SetOption", New Object() {6, 6})
    de.Invoke("SetOption", New Object() {7, 1})
    de.Invoke("SetPassword", New Object() {pwd})
    de.CommitChanges()
    de.Close()
    End If
    

    Help me out and share!

    • Email a link to a friend (Opens in new window) Email
    • Print (Opens in new window) Print
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on Facebook (Opens in new window) Facebook
    • Share on X (Opens in new window) X
    • Share on Reddit (Opens in new window) Reddit
    • Share on Tumblr (Opens in new window) Tumblr
    • More
    • Share on Pinterest (Opens in new window) Pinterest
    • Share on Telegram (Opens in new window) Telegram
    • Share on WhatsApp (Opens in new window) WhatsApp
    Like Loading…

    +

    +

    +

    +

    +

    +

    + Active Directory, Programming, VB, Web, Web Service
    + account, active directory, password, SetPassword, unlock, userAccountControl, vb

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    +

Previous
1 … 12 13 14

Linkedin

Tumblr

GitHub

Mastodon

Create a website or blog at WordPress.com

  • Subscribe Subscribed
    • @JaredMeredith
    • Join 75 other subscribers
    • Already have a WordPress.com account? Log in now.
    • @JaredMeredith
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
%d