Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Using REST to Add another person to the "Assigned To" column in a tasks list

$
0
0

Hello,

I have been recently trying to write some javascript to check the "Assigned To" column of a Tasks list. If the person in question is not currently assigned the task i wish to add them.  I can check the list fine however i cannot seam to append the extra user sucessfully. Here is my current code. In the past i have used spservices for this sort of stuff but i am trying to learn this new way for sp2013. Thanks in advance

function updateAssignedTo(eventObject) {
	var userId = getUserId(eventObject.assignedTo);
	var userAlreadyAssigned = false;
	var assignedTo = [];
    $.ajax({
	    url: _spPageContextInfo.webAbsoluteUrl + eventObject.listWebAddress +"_api/web/lists/getByTitle('Tasks')/items/getById("+eventObject.taskId+")?$select=AssignedToId",
	    method: "GET",
	    headers: {"accept": "application/json;odata=verbose",
	    },
	    success: function (data) {
	        assignedTo = data.d.AssignedToId.results;
	        for (var i = 0; i < assignedTo.length; i++) {
	            if(assignedTo[i] == userId) {
	            	userAlreadyAssigned = true;
	            }
	        }
	        if (userAlreadyAssigned == false) {
	        	var itemType = GetItemTypeForListName("Tasks");
				alert("User Needs to be Assigned");
				assignedTo.push(parseInt(userId));
				var dataToUpdate = {
					__metadata: {type: itemType},
					AssignedToId: assignedTo
				};
				alert(JSON.stringify(dataToUpdate));
				 $.ajax({
					url: _spPageContextInfo.webAbsoluteUrl + eventObject.listWebAddress +"_api/web/lists/getByTitle('Tasks')/items("+eventObject.taskId+")",
					method: "POST",
					contentType: "application/json; odata=verbose",
					data: JSON.stringify(dataToUpdate),
					headers: {"Accept": "application/json; odata=verbose", "X-HTTP-Method":"MERGE", "IF-MATCH": "*",
					},
					success: function (data) { alert("yay"); },
					error: function (data, errorCode, errorMessage) {
					    alert("boo");					
					}
				});
			}
	    },
	    error: function (err) {
	        alert(JSON.stringify(err));
	    }
	});
}



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>