Copy item cu REST


function getListItem(webUrl, listName,listItemId, complete, failure) {
$.ajax({
url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
complete(data.d);
},
error: function (data) {
failure(data);
}
});
}

function createListItem(webUrl,listName, itemProperties, complete, failure)
{
$.ajax({
url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
type: "POST",
processData: false,
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
complete(data.d);
},
error: function (data) {
failure(data.responseJSON.error);
}
});
}

function copyListItem(webUrl,sourceListName,targetListName,listItemId,complete,failure)
{
getListItem(webUrl,sourceListName,listItemId,
function(sourceItem){
//prepare item
var copyItem = {};
for (var propertyName in sourceItem) {
if (sourceItem.hasOwnProperty(propertyName)) {
if(sourceItem[propertyName] != null && sourceItem[propertyName].hasOwnProperty('__deferred')) //exclude deffered objects
{
continue;
}
if(propertyName == 'GUID' || propertyName == 'ID' || propertyName == 'Id')
{
continue;
}
if(propertyName == '__metadata') {
var targetItemType = GetItemTypeForListName(targetListName);
copyItem['__metadata'] = { 'type': targetItemType };
}
else {
copyItem[propertyName] = sourceItem[propertyName];
}
}
}

createListItem(webUrl,targetListName,copyItem,function(targetItem){
complete(sourceItem,targetItem);
},failure);
},
failure);
}

function GetItemTypeForListName(name) {
var itemType = "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
var encItemType = itemType.replace(' ','_x0020_');
return encItemType;
}

copy task item

var sourceListName = 'Tasks';
var sourceListItemId = 7;
var targetListName = 'Archive Tasks';
copyListItem(_spPageContextInfo.webAbsoluteUrl,sourceListName,targetListName,sourceListItemId,printInfo,logError);

function printInfo(sourceItem,targetItem)
{
console.log(String.format('List Item has been copied from {0} to {1}',sourceListName,targetListName));
}

function logError(sender,args)
{
console.log(args.get_message());
}

Ptr campuri explicite

function copyListItem(webUrl,sourceListName,targetListName,listItemId,complete,failure)
{
getListItem(webUrl,sourceListName,listItemId,
function(sourceItem){
//prepare item
var copyItem = {};
var targetItemType = GetItemTypeForListName(targetListName);
copyItem['__metadata'] = { 'type': targetItemType }; //mandatory, do not remove metadata attribute!

//specify explicitly what fields to copy here..
//...
copyItem['Title'] = sourceItem['Title']; //e.g. copy title

createListItem(webUrl,targetListName,copyItem,function(targetItem){
complete(sourceItem,targetItem);
},failure);
},
failure);
}

Anunțuri

Lasă un răspuns

Completează mai jos detaliile tale sau dă clic pe un icon pentru a te autentifica:

Logo WordPress.com

Comentezi folosind contul tău WordPress.com. Dezautentificare / Schimbă )

Poză Twitter

Comentezi folosind contul tău Twitter. Dezautentificare / Schimbă )

Fotografie Facebook

Comentezi folosind contul tău Facebook. Dezautentificare / Schimbă )

Fotografie Google+

Comentezi folosind contul tău Google+. Dezautentificare / Schimbă )

Conectare la %s