SharePoint复制列表项

时间:2017-02-02 14:08:36

标签: c# sharepoint copy listitem

我遇到了复制列表项的问题。你看到了这个错误吗?

当触发ExecuteQuery()时,它会抛出您在本文末尾可以看到的异常。

如果我注释掉foreach(),则没有异常

var spContext = SharePointContextProvider.Current.GetSharePointContext(ctx);
            using (var clientContext = spContext.CreateAppOnlyClientContextForSPHost())
            {
                var list = clientContext.Web.Lists.GetByTitle(listName);
                clientContext.Load(list, l => l.Fields);
                clientContext.ExecuteQuery();

                var sourceItem = list.GetItemById(sourceItemId);
                clientContext.Load(sourceItem);
                clientContext.ExecuteQuery();
                var sourceItemValues = sourceItem.FieldValues;

                var itemCreateInfo = new ListItemCreationInformation();
                var targetItem = list.AddItem(itemCreateInfo);

                foreach (var field in list.Fields)
                {
                    if (!field.ReadOnlyField && !field.InternalName.Equals("Attachments"))
                    {
                        object val = null;
                        sourceItemValues.TryGetValue(field.InternalName, out val);
                        targetItem[field.InternalName] = val;
                    }
                }

                targetItem.Update();
                clientContext.ExecuteQuery();

例外:

Microsoft.SharePoint.Client.ServerException: Invalid request.
   bei Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   bei Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   bei [...]
   bei lambda_method(Closure , ControllerBase , Object[] )
   bei System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   bei System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
   bei System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)

1 个答案:

答案 0 :(得分:0)

解决

无法执行请求,因为不允许更改ContentType。

这一行(取代上面的一行)解决了这个问题:

   if (!field.ReadOnlyField && !field.InternalName.Equals("Attachments") && !field.InternalName.Equals("ContentType"))