It's because I'm new to JavaScript, and lessons don't give what's needed in SharePoint justice so I'm struggling.
I'm trying to run a script that will get details of a selected document(s) and create a task with information on the documents details. I can get the details of the items selected. I just don't understand how to use them. I keep getting errors.
I'm pretty sure it has to do with executeQueryAsync stuff and not using it right to get data to interact with. I'm just blown away on what to do. I freeze. I keep reading but nothing is sinking in. :(
/// <reference path="http://doccenter.photomask.com/SiteAssets/jquery/jquery-2.1.4.min.js" /> // <reference path="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" /> /// <reference path="~/_layouts/15/init.js" /> /// <reference path="~/_layouts/15/MicrosoftAjax.js" /> /// <reference path="~/_layouts/15/SP.Core.js" /> /// <reference path="~/_layouts/15/SP.Runtime.js" /> /// <reference path="~/_layouts/15/SP.UI.Dialog.js" /> /// <reference path="~/_layouts/15/SP.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/init.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/MicrosoftAjax.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/SP.Core.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/SP.Runtime.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/SP.UI.Dialog.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/SP.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/SP.Debug.js" /> /// <reference path="http://doccenter.photomask.com/_layouts/15/SP.Runtime.Debug.js" /> console.log('JavaScript File Loaded'); var items; var list; var clientContext; function MarkAsRead() { console.log('MarkAsRead Function Ran'); var ctxT = GetCurrentCtx(); if (ctxT.listName = 'Documents') { clientContext = new SP.ClientContext.get_current(); //Get Web this.web = clientContext.get_web(); //Get Current User currentUser = web.get_currentUser(); clientContext.load(currentUser); //Get Selected Items this.list = SP.ListOperation.Selection.getSelectedList(clientContext); this.items = SP.ListOperation.Selection.getSelectedItems(clientContext); //clientContext.load(items); this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); } } function onQuerySucceeded(sender, args) { console.log('Query Succeeded'); //currentUser.get_loginName(); console.log(currentUser.get_loginName()); for (i in items) { console.log(items[i].id); } //get workflow task list oList = clientContext.get_web().get_lists().getByTitle('Workflow Tasks'); itemCreateInfo = new SP.ListItemCreationInformation(); this.oListItem = oList.addItem(itemCreateInfo); //oListItem.set_item('ContentTypeId', '0x0108003365C4474CAE8C42BCE396314E88E51F00A154D06FB58C4547999FE55DEA8FE2F90038D1DA75BB246A4A9A2047B864BDDAB1'); oListItem.set_item('Title', 'Read Notification: Javascript'); oListItem.set_item('Body', 'Hello World!'); //"The document,<i>[%Variable: DocumentTitle%]</i>, has been updated\assigned and requires your review. Please review the document by selecting the RELATED ITEM link below. After review, take a moment to edit the task and approve showing you have read the document." oListItem.set_item('AssignedTo', currentUser); oListItem.set_item('Status', 'Completed'); oListItem.update(); clientContext.load(oListItem); clientContext.executeQueryAsync(Function.createDelegate(this, this.onCreateSucceeded), Function.createDelegate(this, this.onQueryFailed)); } function onCreateSucceeded(sender, args) { //console.log("DocumentTitle:" + singleItem.get_item('Title')); console.log("Title:" + oListItem.get_item('Title')); } function onQueryFailed(sender, args) { console.log('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); }
David Jenkins