2

SharePoint 2013. I'm using VS to create document sets in SharePoint. When they're created, I'd like to use a workflow to auto-fill a few columns. However, the workflow won't kick off unless I create doc sets via the GUI. I confirmed that the "Created By" user is my user and NOT System Account whenever I use VS.

I ran this followed by an iisreset. Did not work.

stsadm.exe -o setproperty -propertyname declarativeworkflowautostartonemailenabled -propertyvalue yes

I also ran the following script, which essentially does the same thing.

$spWebService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$spWebService.DeclarativeWorkflowAutoStartOnEmailEnabled = $true
$spWebService.Update()

Didn't work.

I know these scripts worked because workflows now auto start whenever I email a list (woo!).

I have two workflows attached to this library. One kicks off ONLY on item change, and the other (my problem child) is supposed to kick off when an item is created or changed.

I also created a test VS script that edits items in the library, but the workflows still don't kick off. Which makes me believe it's a System Account issue. Even though the "Modified By" account shows my username.

Any advice is appreciated. Thanks in advance!

Edit: I should note, I cannot activate the "Workflow can use app permissions" site feature for some reason. I run into errors and haven't gotten around to resolving it.

Also, here is my code:

using System;
using System.Web;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.BusinessData.MetadataModel.Collections;
using System.Xml;

namespace CRM_Data_Column
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteURL = "http://spsite/";
            using (SPSite siteCollection = new SPSite("http://spsite/"))
            {
                using (SPWeb web = siteCollection.OpenWeb("subsite"))
                {
                    OpenUserContext(web, siteURL, "DOMAIN/username");
                }
            }
        }

    private static void OpenUserContext(SPWeb web, string siteURL, string user)
    {
               SPUserToken userToken = web.AllUsers[user].UserToken;
               SPSite impSite = new SPSite(siteURL, userToken);
               SPWeb impWeb = impSite.OpenWeb("subsite");

        //do things as impersonated user
          }
    }
}

Edit #2:

I figured out that it doesn't work when I create only one item, but sort of works when I create 3 or more.

When I create a bunch of items, the workflow kicks off for all except the last item. Also, the workflow for the second to last item gets stuck on "In Progress". Am I not updating items correctly?

0

You are correct on the system account issue. SharePoint workflows will not run under the system account. You will have to do some type of impersonation if you are going to be running it this way with VS scripts.

  • I am impersonating a user. I've tried my own user, the farm account, another admin, etc. but nothing seems to be working. I'm confirming that impersonation is working by checking out the "Modified By" column, and it changes as expected. – Melyssa Kay Brand Jan 12 '18 at 16:40
0

I added the following code to fix the issue. Still not sure why it wasn't working before.

        private static void StartWorkflow(SPListItem listItem, SPSite spSite, string wfName)
    {
        SPList parentList = listItem.ParentList;
        SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
        foreach (SPWorkflowAssociation association in associationCollection)
        {
            if (association.Name == wfName)
            {
                association.AutoStartChange = true;
                association.AutoStartCreate = false;
                association.AssociationData = string.Empty;
                spSite.WorkflowManager.StartWorkflow(listItem, association, association.AssociationData);
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.