4
votes

I am trying to update a list in a different site using a custom workflow action. The sites are within the same site collection. I have successfully deployed the custom action to the SharePoint server. When I run a workflow that the contains this action, the workflow completes successfully with no errors. I am certain that the action is executing because I see the result of the lines that contain service.LogToHistoryList() in the workflow history log, and they contain the expected values. The problem is that the target list item is not actually being updated. Below is the section of code that is intended to update the list item.

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
Why are you using client object model within custom action? You can use server object model.Yevgeniy.Chernobrivets
Also try to delete clientContext.Load(oListItem); call in foreach loop. I have a feeling then when you call it after update it completely reloads item and overrides all changes.Yevgeniy.Chernobrivets
@Yevgeniy.Chernobrivets I was using the COM because my experience with Sharepoint development is very limited and I just began with C# a couple of days ago. Unfortunately, just removing that line yields the same results. Thanks for your help, your first comment helped me find a solution.cwdj

1 Answers

2
votes

Thank you, Yevgeniy, for your helpful comments. I was able to find a solution using the Server object model as suggested by Yevgeniy. Thanks also to this blog post. The following code solves the problem.

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;
                    }
                }
            }