SharePoint 2010 - Javascript根据标题更新列表项值

时间:2016-01-19 06:23:22

标签: javascript sharepoint

你们有没有想过根据[title]等条件使用javascript更新列表项值以及如何从sharepoint 2010触发javascript?

1 个答案:

答案 0 :(得分:0)

您必须创建JS文件,然后将其添加到您的站点。然后,您必须创建SharePoint网站页面,然后向其添加脚本编辑器webPart。从您将其放在SharePoint中的位置选择JS代码。当文件出现时,从浏览器复制URL(或者以任何方式获取指向该文件的URL)。然后转到您创建的SharePoint页面,单击编辑webPart并添加URL。您可能希望添加用于修改和定位特定列表标题的UI。

在JS代码中,您需要通过执行ExecuteOrDelayUntilScriptLoaded函数来获取对SharePoint SP.js脚本的引用。把它放在你JS文件的顶部。

ExecuteOrDelayUntilScriptLoaded( yourFunctionHere, 'sp.js' ); 

从那里,您需要获取当前的SharePoint上下文,您所在的Web,选择要定位的列表,最后使用上下文对象上的SharePoint executeQueryAsync函数。必须先完成此操作,然后才能操作SharePoint。

function yourFunctionHere(yourSiteWebURL){
        var clientContext, itemCreatInfo, targetList, newListItem;

        // define a ClientContext for the specified SP site.
        clientContext = new SP.ClientContext(yourSiteWebURL);

        clientContext.executeQueryAsync( function(){

            // Grab the SP.ListItemCreationInformation Constructor to add list items.
            itemCreatInfo = new SP.ListItemCreationInformation();

            // Target the desired list.
            targetList =  oWebsite.get_lists().getByTitle('yor targeted list');

            // Assign the targeted list and item ListItemCreationInformation to a variable.
            newListItem = targetList.addItem(itemCreatInfo);

            // Create the list items [Header, Value] format.
            newListItem.set_item('Title', 'new list title value');

            // Update the list.
            newListItem.update();

        });

        clientContext.add_requestFailed(function (sender, args) {
            alert('Request failed: ' + args.get_message());
        }); 
    };