tfforums
tfforums

Reputation: 58

Set "Assigned To" on SharePoint task using Javascript object model

I want to create a sharepoint task and assign it to myself (current user) wtihin the javascript object model. I have the code below, but I think instead of setting a particular user i need to set a spusercollection object. However, I can't seem to find any examples anywhere of how to do this.

  var clientContext = new SP.ClientContext(siteUrl);
	 var web = clientContext.get_web();
   var oList = web.get_lists().getByTitle('Tasks');
 	  
   var itemCreateInfo = new SP.ListItemCreationInformation();
    oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item("Title","My New Item!"); 
    oListItem.set_item("Assigned To", _spPageContextInfo.userLoginName);
	oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );

Upvotes: 0

Views: 1955

Answers (1)

Jameel
Jameel

Reputation: 328

You need to do something like below:

<script type="text/ecmascript">

ExecuteOrDelayUntilScriptLoaded(updateUserField, "sp.js");

function updateUserField(){
    var ctx = new SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle('ListA');
    var item = list.getItemById(1);

    var assignedToVal = new SP.FieldUserValue();
    assignedToVal.set_lookupId(1);   //specify User Id 
    item.set_item("UserField",assignedToVal);
    item.update();

    ctx.executeQueryAsync(
        function() {
            console.log('Updated');
        },
        function(sender,args) {
            console.log('An error occurred:' + args.get_message());
        }
    );
}
</script>

Reference:

https://social.technet.microsoft.com/Forums/en-US/9fac5d83-770f-4d03-8881-f301ea83d8cb/update-person-or-group-field-in-sharepoint-list-using-javascript-object-model?forum=sharepointdevelopmentprevious

Thanks, Jameel

Upvotes: 1

Related Questions