Question

I am experiencing difficulty creating a file in a document library using the client object model. My custom Visual Studio 2010 workflow is not detecting the updates I am making to the file's list item after it is created.

I'd like to give an idea of the infrastructure to answer some likely questions:

  • The document is uploaded to a web service, which is responsible for actually inserting the document in the library and configuring the values of its list columns
  • The web service is performing this operation using the Client Object Model
  • The web service is authenticating against the SharePoint site with an account created for business intelligence automation that DOES NOT RUN as the system account when interacting with SharePoint; it is, however, a member of SharePoint owners
  • The operations in the custom workflow depend on the file's list item columns being populated before it can proceed to assign tasks to the users in two of those columns; for that reason, I created a While activity to monitor changes in the list item until those two columns are no longer null

The following is a sample of what the web service is doing. It is running in IIS under the Business Intelligence user identity. I've added some comments as to which operations I was anticipating the workflow would respond appropriately.

Using client As New ClientContext(My.Settings.SharePointSiteURL)

    // Pre-processing to determine appropriate user ID values for columns

    Dim fci As New FileCreationInformation() With {
        .Content = IO.File.ReadAllBytes(storagePath),
        .Overwrite = True,
        .Url = String.Format("{0}/{1}", theList.RootFolder.ServerRelativeUrl, theFileName)
    }

    Dim theFile As Microsoft.SharePoint.Client.File = theList.RootFolder.Files.Add(fci)

    // Expecting the workflow to be activated here
    client.ExecuteQuery()

    theFile.ListItemAllFields("Project_x0020_Manager") = pmId
    theFile.ListItemAllFields("Project_x0020_Accountant") = paId
    theFile.ListItemAllFields.Update()

    // Expecting the onWorkflowItemChanged activity to be invoked here
    client.ExecuteQuery()
End Using

The workflow does activate when a file is uploaded and continues on to wait for a change event from SharePoint, but that event never arrives as a direct result of the web service's operations. I'm able to modify the item manually and successfully continue on.

Is there a consideration when using the Client Object Model that might preclude these events from firing normally?

Was it helpful?

Solution

Seems like some weird race condition. I was able to partially reproduce your problem with some sample code:

using (var client = new ClientContext(ConfigurationManager.AppSettings["Site"]))
{
    client.Load(client.Site);
    client.Load(client.Site.RootWeb);
    var list = client.Site.RootWeb.Lists.GetByTitle("Shared Documents");
    client.Load(list);
    client.Load(list.RootFolder);
    client.ExecuteQuery();
    var fci = new FileCreationInformation()
              {
                  Content = File.ReadAllBytes(ConfigurationManager.AppSettings["File"]),
                  Overwrite = true,
                  Url = list.RootFolder.ServerRelativeUrl + "/" + "file.xml"
               };
    var file = list.RootFolder.Files.Add(fci);
    client.ExecuteQuery();

    //If following 3 lines are commented out, problem you described may or may not occur. I wasn't able to run into problems, when i loaded file and list item before.
    //client.Load(file);
    //client.Load(file.ListItemAllFields);
    //client.ExecuteQuery();

    //Problem also doesn't occur, when i put here
    //System.Threading.Thread.Sleep(1000); 

    //If code just runs here without delay, sometimes i run into issue you described. 
    var itm = file.ListItemAllFields;
    itm["SampleColumn"] = "Test content!";
    itm.Update();
    client.ExecuteQuery();
}

and workflow:

public Workflow1()
{
    InitializeComponent();
}

public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{
    var comment = "Workflow started!, item SampleColumn is: " +
    workflowProperties.Item["SampleColumn"];
    SPWorkflow.CreateHistoryEvent(workflowProperties.Web, WorkflowInstanceId, 0, workflowProperties.Web.CurrentUser, new TimeSpan(), "Update", comment, string.Empty);
}

private void onWorkflowItemChanged1_Invoked(object sender, ExternalDataEventArgs e)
{
    var comment = "Workflow continous!, item Title is: " +                           workflowProperties.Item["SampleColumn"];
    SPWorkflow.CreateHistoryEvent(workflowProperties.Web, WorkflowInstanceId, 0, workflowProperties.Web.CurrentUser, new TimeSpan(), "Update", comment, string.Empty);
 }

And workflow itself

Workflow screenshot

If you had something different, please let me know. I had myself some problems with declarative workflows which were running on SharePoint library and most of the time i was not able to find nice solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top