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. function getReqData(reqUrl, success, failure) {
  17. $.ajax({
  18. url: reqUrl,
  19. method: "GET",
  20. headers: {
  21. "Accept": "application/json; odata=verbose"
  22. },
  23. success: function(data) {
  24. success(data);
  25. },
  26. error: function(data) {
  27. failure(data);
  28. }
  29. });
  30. }
  31. </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.