使用自定义工作流活动更新同一网站集内不同网站中的列表项

时间:2014-01-16 16:31:12

标签: c# sharepoint

我正在尝试使用自定义工作流操作更新其他网站中的列表。这些网站位于同一网站集中。我已成功将自定义操作部署到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;

        }

1 个答案:

答案 0 :(得分:2)

谢谢你,叶夫根尼,感谢您的有益评论。我能够使用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;
                    }
                }
            }