SharePoint 复制列表项

SharePoint Copy List Item

复制列表项时遇到问题。看到错误了吗?

当 ExecuteQuery() 被触发时,它会抛出异常,您可以在这个 post.

的末尾看到

如果我注释掉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)

已解决

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

这一行(替换上面的行)解决了问题:

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