Friday, 11 January 2013

Update metadata field value

To update metadata field, firstly you have to find the exact value of the metadata value.

Update metadata value manually, then use the following script to find the value:


function queryitems(){
var query = "<Query>";
query += "<Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>Document</Value></Eq></Where></Query>"
$().SPServices({
    operation: "GetListItems",
//webURL: window.location.protocol + "//" + window.location.host,
webURL: "https:" + "//" + url,
    async: false,
    listName: list,
    CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",
CAMLQuery: query,
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_ID") + " : " + $(this).attr("ows_Year") + "</li>";
        $("#tasksUL").append(liHtml);
        var obj = {
        itemid: $(this).attr("ows_ID"),
    };
    arrayid.push(obj);      
      });
    }
  });
}

After get the value, in my case is: 3;#2012, I use the following script to update around 1000 items in a run:
function updateitems(){
    var yearvalue="3;#2012";
    var update_string = "<Batch OnError='Continue'>";
    for (i=0; i<arrayid.length; i++) {
        update_string += "<Method ID='"+(i+1)+"' Cmd='Update'>";
        update_string += "<Field Name='ID'>"+ arrayid[i].itemid+"</Field>";
        update_string += "<Field Name='Year'>"+ yearvalue +"</Field>";
        update_string += "</Method>";
    }
    update_string += "</Batch>";

$().SPServices({
    operation: "UpdateListItems",
        async: false,
        batchCmd: "Update",
        listName: list,
        updates: update_string,
webURL: "https:" + "//" + url,
    completefunc: function (xData, Status) {
        var liHtml = "<li>" + xData.responseText + "</li>";
        $("#Updated").append(liHtml);
    }
  });
}

It is great ~~

No comments:

Post a Comment