Get Manager Of Current User Using Sp.userprofiles And Set It To People And Group Field

In this blog, we will see how to get and set manager property of currently logged in user. We have a one-person editor field and we need to set the name of the manager based on the logged-in user.

I have one visual web part and in that, I have the following control.
  1. <SharePoint:PeopleEditor MultiSelect="false" ValidationEnabled="true" ID="cppFirstApprover" Width="500px" runat="server" VisibleSuggestions="3" Rows="1" CssClass="ms-long ms-spellcheck-true marginleft0" />  
The below-given script is used to get the manager name from SharePoint user profiles.
  1. <script>  
  2.     LoadPeoplePickerDetails();  
  3.     //this function is used to get and set manager name to people editor field of SharePoint  
  4.     function LoadPeoplePickerDetails() {  
  5.         var url = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties"  
  6.         getReqData(url, function(data) {  
  7.             try {  
  8.                 var ManagerName = data.d.UserProfileProperties.results[15].Value;  
  9.                 $("div[id*='cppFirstApprover']").html(ManagerName);  
  10.                 $("a[id*='cppFirstApprover_checkNames']").click();  
  11.             } catch (err) {}  
  12.         }, function(data) {  
  13.             alert("some error occured in getting current User info");  
  14.         });  
  15.     }  
  16.   
  17.     function getReqData(reqUrl, success, failure) {  
  18.         $.ajax({  
  19.             url: reqUrl,  
  20.             method: "GET",  
  21.             headers: {  
  22.                 "Accept""application/json; odata=verbose"  
  23.             },  
  24.             success: function(data) {  
  25.                 success(data);  
  26.             },  
  27.             error: function(data) {  
  28.                 failure(data);  
  29.             }  
  30.         });  
  31.     }  
  32. </script>  
We can also add the above script to document.ready function. Also, we need to save this approver information into Person or Group column SharePoint list.

Suppose, we have a button and on click of this button, we need to save the manager's information into SharePoint list.
  1. <asp:Button ID="btnSave" runat="server" CssClass="btn btn-primary" Text="Submit" OnClick="btnSave_Click1" />  
After creation of the click event, open ascx.cs file and add the below code on button click.
  1. protected void btnSave_Click1(object sender, EventArgs e) {  
  2.     SPSite site = new SPSite(SPContext.Current.Web.Url);  
  3.     using(SPWeb web = site.OpenWeb()) {  
  4.         try {  
  5.             SPList lst = web.Lists.TryGetList("Add your cutom list Name");  
  6.             site.AllowUnsafeUpdates = true;  
  7.             web.AllowUnsafeUpdates = true;  
  8.             SPListItem item = lst.Items.Add();  
  9.             SPFieldUserValueCollection spfcppFirstApprover1 = GetPeopleFromPickerControl(cppFirstApprover, web);  
  10.             item["ApproverName"] = spfcppFirstApprover1;  
  11.             item.Update();  
  12.             site.AllowUnsafeUpdates = false;  
  13.             web.AllowUnsafeUpdates = false;  
  14.         }  
  15.         Catch(Exception ex) {}  
  16.     }  
  17. }  
That's it. We are done here.