Reputation: 61
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;
}
Upvotes: 4
Views: 625
Reputation: 61
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;
}
}
}
Upvotes: 2