Feeds:
Posts
Comments

Archive for September, 2014


Recently had a requirement to update the title of the document with the same name as of document irrespective of what user sets, thought to do it via javascript but then given 2013 changed my mind to give it a try with ECMA script, it took an hour(most of it in finding the name of the document:( ) of troubleshooting but got it working 🙂

Could have done it in lesser steps may be only one query load, but left it in a raw state for others to read through and refine as required 😉

in case someone want to go through some extra steps

UpdateListItemTitle


<script type='text/javascript'>

function getContext()

{

    try

    {

        var context = new SP.ClientContext.get_current(); 

        var list = context.get_web().get_lists().getByTitle('Documents');; 

        var camlQuery = new SP.CamlQuery();

        camlQuery.set_viewXml('');

        this.collListItem = list.getItems(camlQuery);

        context.load(collListItem);

        context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

    }

    catch(exception)

    {

        alert(exception);

    }

}

function onQuerySucceeded(sender, args) {

    try

    {

        var listItemInfo = '';

        var listItemEnumerator = collListItem.getEnumerator();

        while (listItemEnumerator.moveNext()) {

            var oListItem = listItemEnumerator.get_current();

            alert("Updating item with ID-->"+oListItem.get_item('ID')+" with Title-->"+oListItem.get_item('Title')+" To new Title -->"+oListItem.get_item('FileLeafRef'));

            //if(oListItem.get_item('Title')==null){}

            updateListItem(oListItem.get_item('ID'), oListItem.get_item('FileLeafRef'));

        }

    }

    catch(ex)

    {alert(ex);}

}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}

function updateListItem(ItemID, title) {

    var clientContext = new SP.ClientContext.get_current();

    var oList = clientContext.get_web().get_lists().getByTitle('Documents');

    this.oListItem = oList.getItemById(ItemID);

    oListItem.set_item('Title', title);

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededUpdate), Function.createDelegate(this, this.onQueryFailedUpdate));

}

function onQuerySucceededUpdate() {

    alert('Item updated!');

}

function onQueryFailedUpdate(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}

_spBodyOnLoadFunctionNames.push("getContext");

</script>

Read Full Post »