C# SharePoint复制列表项
我复制列表项时遇到问题。你看到错误了吗 当触发ExecuteQuery()时,它会抛出一个异常,您可以在本文末尾看到 如果我注释掉foreach(),则没有例外C# SharePoint复制列表项,c#,sharepoint,copy,listitem,C#,Sharepoint,Copy,Listitem,我复制列表项时遇到问题。你看到错误了吗 当触发ExecuteQuery()时,它会抛出一个异常,您可以在本文末尾看到 如果我注释掉foreach(),则没有例外 var spContext = SharePointContextProvider.Current.GetSharePointContext(ctx); using (var clientContext = spContext.CreateAppOnlyClientContextForSPHost())
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)
Microsoft.SharePoint.Client.ServerException:请求无效。
bei Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(流响应流)
bei Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
bei[…]
bei lambda_方法(闭包、控制器基、对象[])
bei System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
bei System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.b_u39(IAsyncResult asyncResult,ActionInvoke innerInvokeState)
bei System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.c__DisplayClass46.b_u 3f()
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.c__DisplayClass46.b_u 3f()
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uuDisplayClass21.c_uuDisplayClass2B.b_u1C()
bei System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass21.b__1e(IAsyncResult asyncResult)
已解决
无法执行请求,因为不允许更改ContentType
此行(替换上面的行)解决了以下问题:
if (!field.ReadOnlyField && !field.InternalName.Equals("Attachments") && !field.InternalName.Equals("ContentType"))
目标项目是什么?我只得到Id、DisplayName、FieldValue等属性。。。但不是。Item?
ListItem。Item
(根据)是用于通过内部名称访问字段值的属性。与我之前的评论相反,您可以使用括号表示法来访问它,比如targetItem.Item[field.InternalName]=val代码>