locked
Trigger event when workflow is added to a document library RRS feed

  • Question

  • using Moss 2007;

    I've created a custom sequential workflow using Visual studio and wanted to create SharePoint group when the workflow is added to a document library. Please let me know how do we do that?


    --Cheers
    Tuesday, September 20, 2011 12:39 PM

Answers

  • If you want to have only one association at a time then you can link an association page to the workflow and the purpose of this will be that as soon as you click on Associate a workflow to your library then for that particular library only it will create a group with the name of the library.

    I have tested it at my end and it is working. Don't know whether you will like to go with this approach or not but its very simple.

    You just need to add an association page in your workflow. On add association button click just add the following piece of code

    SPList list = Web.Lists[associationParams.TargetListGuid];
                    Web.SiteGroups.Add("Group"+list.Title, user, user, "");
    


    And it is done. It worked for me. 

     

    • Marked as answer by Prasath C Monday, September 26, 2011 11:33 AM
    Wednesday, September 21, 2011 4:43 PM

All replies

  • You can have a code activity inside your workflow that will contain the code to create a group to the sharepoint site.

    http://sharepointxperience.blogspot.com/2007/09/add-new-spgroup-programmatically-to.html

    http://www.dotnetspark.com/kb/4121-programmatically-create-sharepoint-group.aspx

     

    These links show how to create groups programmatically. Implement the same logic inside a code activity.

    Hope it helps!

    Regards,

    Geetanjali

    Tuesday, September 20, 2011 5:33 PM
  • I'm fine with creating groups programmatically, but what I need is, trigger an event/method when the workflow is added to the document library. 
    --Cheers
    Wednesday, September 21, 2011 5:25 AM
  • As far as I understand you want to trigger the workflow as soon as you attach it to a library and not when an item is created in the library.

    Is this what you mean? 

    Wednesday, September 21, 2011 6:55 AM
  • Yes.
    --Cheers
    Wednesday, September 21, 2011 8:55 AM
  • I have actually never implemented a scenario like yours as to initiate a workflow at list level not an individual list item level. Can you please explain me the exact scenario that you want to achieve so that I can have a better vision about it and then think of some approach that suits your requirement.
    Wednesday, September 21, 2011 9:33 AM
  • Thanks for looking in to this.

    I'm working on a multilevel approval workflow and need to create 2 SharePoint groups for Approvers and Reviewers. I want to create these SharePoint groups when I attach a workflow to a library, because users in these groups may vary for each document library. When a item is added/edited in the library, the workflow is invoked and send email notification to the users in the groups for further approval.

    I've completed the workflow, currently creating the groups manually.

    The Process is

    1. Administrator attach this multilevel workflow to a document library

    2. Add users to the groups created when the workflow is attached.

    3. When an item is added or edited, email notification is sent to users in the group for further approval process


    --Cheers
    • Edited by Prasath C Wednesday, September 21, 2011 9:52 AM
    Wednesday, September 21, 2011 9:52 AM
  • Thanks for the detailed information. I was just wondering if the following scenario will work for you or not.

    When you will deploy your custom workflow through a feature then at that feature itself you can apply an event receiver which will create the groups for you. So that feature will have two roles. Deploy your workflow and attach it to the library and create groups.

    Now the groups can be created in this manner. As far as dynamically changing users based upon Document Library then I am not very sure as there can be so many document libraries. As a workaround I can think of that if there is just a couple of fixed document library then based upon which document library the workflow is attached to we can add users to the group. 

    So my approach is in the event receiver , create an SPGroup. Now when you start the workflow on item adding or editing , just before you have your SendEmail activity, insert a CodeActivity that will check to which document library the workflow is attached and then add users to the group based upon that. 

    I am not very sure about this approach as i haven't tried but its always better to keep trying. :)

    Wednesday, September 21, 2011 11:15 AM
  • Thanks again. Currently the feature is scoped at site collection level. So if we create group when we activate the feature, we can create only one set of group. Our requirement is to create the groups for each document library. This group should be unique for each document library at various site level. So I don't believe event receiver will work for my requirement. 
    --Cheers
    Wednesday, September 21, 2011 11:42 AM
  • Well, in such a situation I can think of one thing that when the workflow starts for the first item that you add to the document library at that time before sending emails , include a code activity and there see whether for that document library the group exists at the scope of the site.

    To name a unique group, ApproversForDocLibraryName something like this. So at the site level you will have unique groups for document libraries.

    Inside your code activity , first check to which document library is the instance running, then create a unique group name as I already mentioned. Check if the group exists with that name at site scope. If exists then do nothing, else create a group and add users to the group. After that in next activity you can have the SendEmail activity. So for the very first time an item gets created and the workflow starts , it will create the group and add users corresponding to that specific library. For the second time when you add the item and the workflow starts then first it will check if the group exists corresponding to that document library, and since it exists then it will do nothing and proceed to next activity.

    Please let me know if it suits your requirement.

    Wednesday, September 21, 2011 12:48 PM
  • Thanks again.

    This is what I did and the issue with this model is, I don't want to add users programmatically. only wanted to create groups. Users will be added manually after the groups are created when the workflow is attached to the document library. Please see the process in my previous post.

     

     


    --Cheers

    • Edited by Prasath C Wednesday, September 21, 2011 1:09 PM
    Wednesday, September 21, 2011 1:06 PM
  • Well can you just try one thing. I just saw that SPList has a property named WorkflowAssociations. So if you use WorkflowAssociations.GetAssociationByName() on a list/document library then you can see whether the workflow has been associated to your library.

    The process will be something like this then.

    • Feature that will associate your workflow to all the document library
    • Once the above feature is activated then go to Site Features and activate the GroupActivationFeature. This feature will be scoped at site level and it will query all the document libraries in that site and see if WorkflowAssociations.GetAssociationByName("Workflow name") returns an object i.e. the document library has the workflow attached. Then for each document library that has the workflow attached, it will create the group.
    • Manually add users to the groups created
    • Your workflow logic on item adding/editing that will start the workflow.

     

    I hope this makes some sense. 

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                
                //logic to fetch SPListCollection for document library
               SPListCollection docLibs = some collection based on logic
               
               foreach(SPList list in docLibs)
                {
                      SPWorkflowAssociation assoc = list.WorkflowAssociations.GetAssociationByName("WWorkflow Name");
                    
                     if(assoc != null)
                     {
                          //Logic to create SPGroup
                     }
                }
            }
    


    Try something like this and see if it suits your requirement. The only thing that is required is that you need to have feature to associate your workflow to document library activated first. After that you activate this feature. 

    Please let me know if it helps.

    Wednesday, September 21, 2011 2:51 PM
  • Please see my comments below on your suggestion

    • Feature that will associate your workflow to all the document library

      Do you mean adding my workflow to the workflow list for all document library? If so, the feature that I've created already will associate the workflow to all the document libraries on activation.
       
    • Once the above feature is activated then go to Site Features and activate the GroupActivationFeature. This feature will be scoped at site level and it will query all the document libraries in that site and see if WorkflowAssociations.GetAssociationByName("Workflow name") returns an object i.e. the document library has the workflow attached. Then for each document library that has the workflow attached, it will create the group.

      I believe this method will create groups for all document libraries when the feature is activated. I don't want this to happen as the workflow is added to one document library at a time. The groups should be created only when we add a workflow to a document library.
       

    There may be other workaround like this, but I wanted something simple as we do for other built-in workflow. Hope you understand. Thanks for your suggestions.


    --Cheers
    • Edited by Prasath C Wednesday, September 21, 2011 4:05 PM
    Wednesday, September 21, 2011 3:46 PM
  • If you want to have only one association at a time then you can link an association page to the workflow and the purpose of this will be that as soon as you click on Associate a workflow to your library then for that particular library only it will create a group with the name of the library.

    I have tested it at my end and it is working. Don't know whether you will like to go with this approach or not but its very simple.

    You just need to add an association page in your workflow. On add association button click just add the following piece of code

    SPList list = Web.Lists[associationParams.TargetListGuid];
                    Web.SiteGroups.Add("Group"+list.Title, user, user, "");
    


    And it is done. It worked for me. 

     

    • Marked as answer by Prasath C Monday, September 26, 2011 11:33 AM
    Wednesday, September 21, 2011 4:43 PM
  • It worked. This is what exactly I'm looking for. Thank you Geetanjali. I really appreciate your help.
    --Cheers
    Monday, September 26, 2011 11:33 AM