2014-01-16 36 views
4

我試圖使用自定義工作流操作更新不同網站中的列表。這些網站位於同一網站集內。我已成功將自定義操作部署到SharePoint服務器。當我運行包含此操作的工作流程時,工作流程成功完成且沒有錯誤。我確定該操作正在執行,因爲我在工作流程歷史日誌中看到包含service.LogToHistoryList()的行的結果,並且它們包含期望值。問題是目標列表項目實際上未被更新。以下是用於更新列表項目的代碼部分。使用自定義工作流程活動更新同一網站集內其他網站中的列表項

try 
     { 
      ISharePointService service = (ISharePointService)executionContext.GetService(typeof(ISharePointService)); 
      service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.Zero, "Debugging Output", SiteUrl, string.Empty); 
      service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.Zero, "Debugging Output", List, string.Empty); 
      service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.Zero, "Debugging Output", Column, string.Empty); 
      service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.Zero, "Debugging Output", Value, string.Empty); 
      service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.Zero, "Debugging Output", updateCol, string.Empty); 
      service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.Zero, "Debugging Output", updateVal, string.Empty); 

      ClientContext clientContext = new ClientContext(SiteUrl); 
      SP.List oList = clientContext.Web.Lists.GetByTitle(List); 

      CamlQuery camlQuery = new CamlQuery(); 
      camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='"+Column+"'/>" + 
       "<Value Type='String'>"+Value+"</Value></Geq></Where></Query><RowLimit>1</RowLimit></View>"; 
      ListItemCollection collListItem = oList.GetItems(camlQuery); 

      clientContext.Load(collListItem); 
      clientContext.ExecuteQuery(); 

      foreach (ListItem oListItem in collListItem) 
      { 
       oListItem[updateCol] = updateVal; 
       oListItem.Update(); 
       clientContext.Load(oListItem); 
       clientContext.ExecuteQuery(); 
      } 


      return ActivityExecutionStatus.Closed; 
     } 
     catch (Exception ex) 
     { 
      ISharePointService service = (ISharePointService)executionContext.GetService(typeof(ISharePointService)); 

      if (service == null) 
      { 
       throw; 
      } 

      service.LogToHistoryList(this.WorkflowInstanceId,SPWorkflowHistoryEventType.WorkflowError, 0, TimeSpan.Zero,"Error Occurred", ex.Message, string.Empty); 
      return ActivityExecutionStatus.Faulting; 

     } 
+0

爲什麼在自定義操作中使用客戶端對象模型?您可以使用服務器對象模型。 –

+0

也嘗試刪除clientContext.Load(oListItem);在foreach循環中調用。我有一種感覺,當你在更新後調用它時,它會完全重新加載項目並覆蓋所有更改。 –

+0

@ Yevgeniy.Chernobrivets我使用的是COM,因爲我在Sharepoint開發方面的經驗非常有限,而且我剛剛在幾天前開始使用C#。不幸的是,只是刪除該行會得到相同的結果。感謝您的幫助,您的第一條評論幫助我找到了解決方案。 – cwdj

回答

2

謝謝Yevgeniy,爲您提供有用的意見。我能夠按照Yevgeniy的建議使用服務器對象模型找到解決方案。 Thanks also to this blog post。下面的代碼解決了這個問題。

using (SPSite _site = new SPSite(SiteUrl)) 
      { 
       using (SPWeb _web = _site.OpenWeb()) 
       { 
        SPList oList = _web.Lists[List]; 
        SPQuery _query = new SPQuery(); 

        _query.Query = "<Where><Eq><FieldRef Name='"+Column+"' /><Value Type='Text'>"+Value+"</Value></Eq></Where>"; 
        SPListItemCollection _itemCollection = oList.GetItems(_query); 

        if (_itemCollection.Count > 0) 
        { 
         _web.AllowUnsafeUpdates = true; 

         foreach (SPListItem Item in _itemCollection) 
         { 
          Item[updateCol] = updateVal; 
          Item.Update(); 
         } 

         _web.AllowUnsafeUpdates = false; 
        } 
       } 
      }