1

How to access List Item's Property Bag?

I am trying to replace all properties from one item to another.

var oldItems = oldList.getItems(query);
var newItems = newList.getItems(query);
context.load(oldItems);
context.load(newItems);

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

...

oldDoc = oldItems.getById(10);
newDoc = newItems.getById(4);
context.load(oldDoc);
context.load(newDoc);

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

...

var mi = newDoc.get_item('MetaInfo');
oldDoc.set_item('MetaInfo', mi);
oldDoc.update();

context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId), Function.createDelegate(this, this.onQueryFailed));
0

This clones an Item (By id) to a new Item in the same List:
So needs some refactoring to set the get_fields() in an existing Item in the getEnumerator loop

sorry for the short notations, this came from code stuck into a Calculated Column Formula, so had to be minified (because every item added the same code to the page)

function cloneItem(id) {
    var c = new SP.ClientContext.get_current(),
        l = c.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList()),
        i = l.getItemById(id),
        f = l.get_fields(),
        n, e, d, m, v,
        url = _spPageContextInfo.serverRequestPath;
    c.load(i);
    c.load(f);
    c.executeQueryAsync(
        function () {
            n = l.addItem(new SP.ListItemCreationInformation());
            e = f.getEnumerator();
            while (e.moveNext()) {
                d = e.get_current();
                m = d.get_internalName();
                if (!(d.get_readOnlyField() 
                       || d.get_hidden() 
                       || ['Attachments', 'ContentType'].indexOf(m) > -1)
                   ) {
                    v = i.get_item(m);
                    if (v) n.set_item(m, v);
                }
            }
            n.update();
            c.load(n);
            c.executeQueryAsync(function () {
                document.location = url.substr(0, url.lastIndexOf('/')) 
                                     + '/EditForm.aspx?ID=' + n.get_id();
            })
        })
}
2
  • Read question before answering :-) – user64490 Feb 13 '17 at 14:20
  • Okay, sorry. I misunderstood then. Thought you wanted to use JSOM to copy properties from one item to another. – Danny '365CSI' Engelman Feb 13 '17 at 14:51
0

You should just replace following string of the code

var mi = newDoc.get_item('MetaInfo');

with this

var mi = newDoc.get_fieldValues().MetaInfo;

And your solution will be work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.