设置"分配给"使用Javascript对象模型的SharePoint任务

时间:2016-08-04 00:16:07

标签: javascript sharepoint

我想创建一个sharepoint任务,并将其分配给我自己(当前用户)的javascript对象模型。我有下面的代码,但我认为我需要设置一个spusercollection对象,而不是设置一个特定的用户。但是,我似乎无法找到任何有关如何执行此操作的示例。



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




1 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

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

参考:

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

谢谢, 贾米尔