Search code examples
sharepointworkflowcode-behindcustom-action

Start a workflow from a custom button/action on a list item in SharePoint 2010 (all with Visual Studio 2010)


I'm sorry for asking a double question, but I'm messed up with finding no solution - or a missing understanding of answers.

My problem is like descrided:

I have some self constructed custom ribbons with buttons. And I like to start a workflow if I click on designed button.

Okay, more about settings:

It is a generic list, means, my elemets.xml looks like that:

<CustomAction
  Id="MyCustomRibbonTab"
  Location="CommandUI.Ribbon.ListView"
  RegistrationId="100"
  RegistrationType="List">
  <CommandUIExtension>
    <CommandUIDefintions>
      <CommandUIDefinition
        Location="Ribbon.Tabs._children">
        <Tab Id="Ribbon.CustomTab" Sequence="501">
        ... (Scaling)
          <Groups Id="Ribbon.CustomTab.Groups">
            <Group 
              Id="Ribbon.CustomTab.GroupOne" 
              Sequence="52">
              <Controls Id="Ribbon.CustomTab.GroupOne.Controls">
                <Button
                  Id="Ribbon.CustomTab.GroupOne.ButtonOne"
                  Command="CustomTab.ButtonOneCommand"
                  Sequence="11">
              </Controls>
            </Group>
          </Groups>
        </Tab>
      </CommandUIDefinition>
    </CommandUIDefinitions>
    <CommandUIHandlers>
      <CommandUIHandler
        Command="CustomTab.ButtonOneCommand"
        CommandAction="javascript:alert('how start workflow here?');" />
        />
    </CommandUIHandlers>
  </CommandUIExtension>
  <!-- what about starting workflow here? How? -->
</CustomAction>

Hope it is clear what my XML-Post means. Okay, that works fine if I put CommandAction="javascript:alert('should start a workflow');" . I mean, this alert pops up and I can click okay, nothing else.

Now I have in same project my workflow designed. There are two types, one with initiation form in asp, the other without, both operating on selected item. I can start them while using the standard ribbon "Workflows" and then click them. Everything is fine so far.

But I want to start each workflow by clicking my button, not having more clicks as it is now. Thank you so far in helping me.

I'm very new on SharePoint. So please include in your answer stuff like "Add new ... Empty Element/ JavaScript File" or whatever is neccessary to get what I assume.

If you have more questions about my workflow design, I will answer asap.

Thank you very much,

danbruc

Note: Yes, I just hope that even my Elements.xml is fine. I took all from web. So if yuo have suggestions to redesign, I am open minded. Just blind in seeing the solution. Additionally, I'm able to put this Tab inside ContextualTab of ListView, but then even javascript is not working.


Solution

  • Did you read the documentation on Customizing and Extending the SharePoint 2010 Server Ribbon?

    It is very concise and shows you an important points concerning your problem: You will need to do a postback to initiate the workflow via serverside code:

    <CommandUIHandler Command="WebPartContextualTabs.OnPostback"
    CommandAction="javascript:__doPostBack('StartMyWorkflowPostBack','');" />
    

    Now what to put in StartMyWorkflowPostBack to start your workflow? There are several resources for this and you will find plenty more via Google:

    Let me explain in short: You did not tell us what kind of workflow. A list based workflow or a site workflow, start methods differ for these. Taking the list workflow as an example you will need to just start the workflow like so:

    SPWorkflowProperties.Site.WorkflowManager.StartWorkflow(listItem, associationTemplate, initData);
    

    But as you can see you will need a lot of stuff for this: The SPListItem, the SPWorkflowAssociationTemplate as well as some initiation data.