1

I've got a document library and want to add the department filled from AD, so the library can be sorted by department. How can I do this?

I tried to add column of type "Person or group" and show field "department". It is not filled

1
  • Could you pls check if department property is mapped to AD field or not in central admin?
    – vijayata
    May 6, 2016 at 7:35

1 Answer 1

1

Please check this: http://www.vrdmn.com/2013/07/sharepoint-2013-get-userprofile.html

I get the department with this script:

<script type="text/javascript">

$(document).ready(function() {
var loginName = "";
var userid = _spPageContextInfo.userId;

function GetCurrentUser() {

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";

var requestHeaders = { "accept" : "application/json;odata=verbose" };

$.ajax({
  url : requestUri,
  contentType : "application/json;odata=verbose",
  headers : requestHeaders,
  success : onSuccessA,
  error : onErrorA
});
}

function onSuccessA(data, request){

    // alert("Success One");
    // cutting of i:0#.w|

    //console.log(data.d);
    //var emailAdresse = data.d.Email;
    //alert(emailAdresse);

    var loginName = data.d.LoginName.split('|')[1];

    var repOne = "%5C";  
    var repTwo = "%2E";
    // alert("before replace: " + loginName);

    loginName = loginName.replace(/\\/g, repOne); // replace \ with %5C
    loginName = loginName.replace(/\./g, repTwo); // replace . with %2E

    // alert("after replace: " + loginName);

    GetCurrentUserDepartment(loginName); // call next function to get department. Pass the loginname into the function
}

function onErrorA(error) {

  alert(error);
}

GetCurrentUser();

});

function GetCurrentUserDepartment(loginNameAcc) {
// alert("LoginName: " + loginNameAcc)
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Department')?@v=" + "'" + loginNameAcc + "'";

var requestHeaders = { "accept" : "application/json;odata=verbose" };

$.ajax({
  url : requestUri,
  method: "GET",
  headers : requestHeaders,
  success : onSuccessB,
  error : onErrorB
});
}

function onSuccessB(data){

    //do someting

}

function onErrorB(jqxr,errorCode,errorThrown){
        alert("Error: " + jqxr.responseText)
}

</script>

I am using this, to set a link on the startpage to the specific department teamsite.

Hope this will help.

7
  • can this code be put into calculated column ? or so. As i'm not allowed to use visual studio here to "program" against sharepoint. As if possible i hope above code would fill Department once (upon creation), of the document, and wont change as document property if someone changes deparment (promotion) or so.
    – Peter
    May 6, 2016 at 8:10
  • You can add an script editor webpart or an content editor webpart and paste the code inside of that. Please also check the mentioned link.
    – Patrick
    May 6, 2016 at 8:12
  • For setting metadata in documents you can use a workflow which runs on item creation or change for a specific content type. That could solve that.
    – Patrick
    May 6, 2016 at 8:14
  • I'm trying with sharepoint designer, but no luck either. I am using set field (my department column) to to the person who created this item, and from that userprofile retrieve Department. Doesnt work either
    – Peter
    May 6, 2016 at 10:24
  • Are you getting the department via the rest url? Then you should be able to get it in an SharePoint Designer 2013 workflow.
    – Patrick
    May 6, 2016 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.