0

I followed this blog - Pass user information from SharePoint 2013 to InfoPath 2013 It's a really good solution and works well but I want to expand it and get the users department and office location.

I thought it would just be a case of adding this at line 16

document.getElementById('userOffice').innerHTML = user.get_Office();

Then at line 22

url += '4[' + encodeURIComponent(user.get_office()) + ']';

Then at line 44

<span id="userOffice"></span>

But it doesn't seem to be the case. Can I do this or am I going about this the completely wrong way?

(sorry to send you off to a link - I would have pasted the whole code into this question but it wasn't having it for some reason)

Thanks Jamie

1
  • have you found your answer? If so, please let us know or give us some feedback
    – Patrick
    Commented Sep 18, 2015 at 8:49

2 Answers 2

0

I am did something similar with REST to get the users department.

Then I set a link depending on the department he/she is in, onto my page.

<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|
        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){

        var department = data.d.GetUserProfilePropertyFor;
        // alert("Team:" + department);
        var departmentArray = new Array(    
            "Information Technology",
            "Human Resources");

        var departmentArrayURL = new Array(     
            "information-technology-workspace",
            "human-resources-workspace");

        // set link to logged in users department
        switch(department){
            default:
                // alert("case default");
                break;
            case departmentArray[0]:
                // alert("case Information Technology");
                document.getElementById("teamuebersicht").innerHTML = "<a href='/teams/"+departmentArrayURL[0]+"'>Workspace</a>";
                break;
            case departmentArray[1]:
                // alert("case Human Resources");
                document.getElementById("teamuebersicht").innerHTML = "<a href='/teams/"+departmentArrayURL[1]+"'>Workspace</a>";
                break;
            }    
    }

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

</script>
0

Instead of loading it using the REST I used the SharePoint OOTB control to get it on page load. I registered the namespace in masterpage like

<%@ Register TagPrefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

and to get a profile property, write some where in masterpage like

<script type="text/javascript">
                var officeId = '<SPSWC:ProfilePropertyValue PropertyName="Office" ApplyFormatting="False" TitleMode="True" runat="server"/>';
                console.log(officeId);
            </script>

put your property name in place of "PropertyName" attribute value to get the value

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.