3

I am just starting to use SPServices and have no idea how to get the current item/task assignedTo user's email id. I was searching for 2 days and didn't find a proper/easy solution. I am trying to get that in a CEWP in SharePoint 2013.

I found this script to get the ID of the current item and am struggling to use this id and get the AssignedTo user's email id

<script type="text/javascript">

    var docurl = document.URL;
    var beginindex = docurl.indexOf('?ID=') + 4;
    var endindex = docurl.indexOf('&Source=');
    var itemid = docurl.substring(beginindex, endindex);    

</script>

Please help me.

7
  • Is SPServices really required? you can use plain JSOM with your requirement. Where are you inserting your CEWP?
    – Aventus
    Commented Aug 24, 2016 at 6:08
  • in task edit form. i want to compare the current logged in user and the assignedTo user. If not same, i am disabling the "Approve" and "Reject" button controls. Because SharePoint allows anyone to approve or reject the tasks. I used auto start workflow to set the permissions to that AssignedTo only, but it is not auto starting due to App step / system account. Commented Aug 24, 2016 at 6:17
  • Users will only have write and read items. They wont be able to edit the page.. so i thought this is the best i can think of achieving allowing the assignedTo users to take action on the task. I am not sure if there is another way to do this. It's really important for me to allow only the assignedTo users.. If you know any other foolproof solution.. please share.. thanks Commented Aug 24, 2016 at 6:21
  • I did something similar to this but I didn't used SPServices, is SPServices your preferred approach?
    – Aventus
    Commented Aug 24, 2016 at 6:22
  • i counld't find any other way.. if i get the assingedTo's email id using some java script would be fine.. rest i have it ready.. once i get this email id, i can compare and disable the controls Commented Aug 24, 2016 at 6:25

1 Answer 1

2

Based on your comments, you are open with other options other than SPServices and you just want to compare the current user with the user in the Assigned To field, I have a similar requirement before.

You should reference jQuery in order for this to work

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(DisableButtons, "sp.js");
});

function DisableButtons() {
var ID = getQueryStringParameter("ID");
var _itemUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('List Title')/getItemById(" + ID + ")?$select=AssignedToId";
$.ajax({
    url : _itemUrl,
    method : "GET",
    headers : {
        "accept" : "application/json;odata=verbose"
    },
    success : function (data) {
        if (data.d.AssignedToId != null && data.d.AssignedToId != _spPageContextInfo.userId) {
            //disable Approve and Reject button
        }
    },
    error : function (error) {}
});

}

function getQueryStringParameter(paramToRetrieve) {
try {
    var _params = '',
    _singleParam = '';
    _params = document.URL.split("?")[1].split("&");
    for (var i = 0; i < _params.length; i = i + 1) {
        _singleParam = _params[i].split("=");
        if (_singleParam[0] === paramToRetrieve)
            return _singleParam[1];
    }
} catch (e) {}
}
6
  • i am getting [object object] for data.d.AssignedToId Commented Aug 24, 2016 at 7:13
  • can you show it in console.log and expand, what are the values?
    – Aventus
    Commented Aug 24, 2016 at 7:14
  • there is only one value for 0 to 17 Commented Aug 24, 2016 at 7:27
  • does your assigned to field have multiple value?
    – Aventus
    Commented Aug 24, 2016 at 7:30
  • no, it contains only one value, but think the field is a multi value field Commented Aug 24, 2016 at 7:31

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.