Saturday, November 19, 2011

The easiest way to package and deploy a Sharepoint workflow

Note: this relates to packaging up MOSS 2007 workflows created in Visual Studio.

If you have developed your workflow in Visual Studio and have already been F5 deploying it (which means you have already created your feature.xml and workflow.xml), and now you are ready to package it up into a wsp for deploying to other servers, then the following is probably the quickest way of doing that.

Essentially, it involves using WSP Builder. However, this method doesn't require using any of the WSP Builder project templates to do it. The templates are useful if you create your workflow project from the WSP Builder templates to begin with. But if you created your workflow with the normal Visual Studio workflow templates, THEN want to use WSP Builder templates for packaging the workflow, its a bit of a pain to do. This will enable you to get your .wsp with the absolute minimum in changes to your Visual Studio project.

1. install WSP Builder, then load Visual Studio and your workflow project

2. in your Project, create 12/TEMPLATE/FEATURES/your_feature_name

3. copy feature.xml, workflow.xml, and your InfoPath task forms (.xsn's) into this folder

4. right-click your project, click WSPBuilder -> Build WSP

WSP Builder will automatically
1. build your manifest.xml for you, adding in everything it sees in your 12 folder structure
2. get your project's assembly (.dll)
3. package it all into a .wsp file and drop it in the root of your project


Below are my installation and removal scripts.


rem Workflow installation script

d:\stsadm -o addsolution -filename d:\deploy\MyWorkflowProject\MyWorkflowProject.wsp

d:\stsadm -o deploysolution -name MyWorkflowProject.wsp -immediate -allowGacDeployment

pause

d:\stsadm -o activatefeature -name MyWorkflowProject -url http://server_to_deploy_to



rem Workflow removal script

d:\stsadm -o deactivatefeature -name MyWorkflowProject -url http://server_to_deploy_to

d:\stsadm -o retractsolution -name MyWorkflowProject.wsp -immediate

pause

d:\stsadm -o deletesolution -name MyWorkflowProject.wsp -override

Tuesday, October 18, 2011

How to email members of a SharePoint group

This relates to MOSS 2007.

In doing a state machine workflow, I have noticed how you can assign a SharePoint group to a task no problem, but when you set that group as the recipient of an email activity, you get an error. I wrote a very simple function that takes in the SharePoint group's name as a parameter and returns a semi-colon delimited string of the email addresses of the people in the group. I also had another scenario where I wanted to get the login names of the individuals in the group, too, so I made another parameter that acts as a switch - you choose whether you want login names or email addresses. Just assign the function below to your email task's To property and you are good to go!

Private Function GetGroupInfo(ByVal GroupName As String, ByVal WhatToGet As String) As String
'PURPOSE: returns semicolon-separated string of group member login names or email addresses

Dim strReturnString As String = ""

'get group
Dim TheGroup As SPGroup = workflowProperties.Web.SiteGroups(GroupName)

'loop through group and build string of login names or email addresses
For Each GroupMember As SPUser In TheGroup.Users

If WhatToGet = "LOGIN_NAMES" Then

If strReturnString = "" Then
strReturnString = GroupMember.LoginName
Else
strReturnString = strReturnString & "; " & GroupMember.LoginName
End If

ElseIf WhatToGet = "EMAIL_ADDRESSES" Then

If strReturnString = "" Then
strReturnString = GroupMember.Email
Else
strReturnString = strReturnString & "; " & GroupMember.Email
End If
End If
Next

Return strReturnString

End Function


To get the email addresses of members in a SharePoint group called "Account Personnel" and then assign them to an email activity's To property, type:
MyTaskEmail_To = GetGroupInfo("Account Personnel", "EMAIL_ADDRESSES")

Workflow task link in email

Just wanted to do a quick post on a recent gotcha. I created a MOSS 2007 state machine workflow and added custom email activities to send emails notifying users of their tasks. I wanted to have a link in those emails that pointed directly to the user's task, ie the same link you get when you enable "Send e-mail when ownership assigned?" in the task list's Settings -> List Settings -> Advanced settings screen.

I thought I could simply reference the task's ListItemId property, but it kept coming up as -1. After finding an article on SharePoint Tricks, I realized I needed to create a Field variable for the ListItemId property, then reference the Field variable.

1. In the create task activity, create a Field from the ListItemId (by default it says -1).

2. The link in the email concatenates workflowProperties.SiteUrl, workflowProperties.TaskListUrl, "/DispForm.aspx?ID=", and finally createMyApprovalTask_ListItemId.ToString()


This will link directly to the user's task form on the SharePoint site.

That's it!

Wednesday, September 28, 2011

How to remove a SharePoint solution that is giving an error

A while back I had a solution that would just not be removed. In the Solution Management screen in Central Administration, the Status for the solution was a bright red "Error". Attempting to remove it from there would give an error.

Luckily, I found a great post by Alex Thissen that clearly explains how to use stsadm to remove a malfunctioning solution. The blog post is called Remove malfunctioning Windows SharePoint Services solutions.

In a nutshell, if retracting the solution:

stsadm -o retractsolution -name solutionname.wsp -immediate

doesn't work, and if deleting the solution:

stsadm -o deletesolution -name solutionname.wsp -override

doesn't work, then

1. run an enumeration to see the scheduled deployments

stsadm -o enumdeployments

2. use the JobId to cancel the deployment

stsadm -o canceldeployment -id 2529c788-971c-46a3-b69f-a2a0a1fcc851


Afterwards, you can verify it by doing 1. again.

Thursday, September 22, 2011

How to start a SharePoint State Machine workflow without association or initiation forms

This assumes you are creating a SharePoint 2007 state machine workflow in Visual Studio.

If you ever need a workflow to start, but don't want it to have an association or initiation form, you have to do two things, both of which are in workflow.xml:

1. In the Workflow element, do not specify an AssociationUrl (for the association form) or InstantiationUrl (for the initiation form). Just leave it out of the element completely! You may want to do the same thing if you have no modification form.
The attributes indicate the web forms that will host the association and initiation forms. By default, they look like AssociationUrl="_layouts/CstWrkflIP.aspx" and InstantiationUrl="_layouts/IniWrkflIP.aspx".


2. In the MetaData element, be sure Association_FormURN and Instantiation_FormURN are commented out.

Sunday, June 26, 2011

How to create and deploy a web-enabled InfoPath form with managed code

In this article, we will create a web-enabled InfoPath form that will have managed code in its "on load" that will populate a field automatically. We will deploy our InfoPath form to a SharePoint form library.

First off: this article assumes you are using MOSS 2007, Microsoft Office InfoPath 2007, and Visual Studio 2008. To add managed code to an InfoPath form, Visual Studio Tools for Applications (VSTA) must be installed.

If VSTA is not set up, the following message appears:

"InfoPath cannot add the event handler.

To work with Visual Basic or C# code, VSTA is required. .NET Framework 2.0 and Microsoft Core XML Services 6.0 must be installed before VSTA."

To install VSTA:
----------------
1. Control Panel -> Add or Remove Programs -> Microsoft Office 2007 -> Change

2. Under Microsoft Office InfoPath -> .NET Programmability Support, check the .NET Programmability Support for .NET Framework version 2.0 and then under that check Visual Studio Tools for Applications

Also, be sure Microsoft Core XML Services 6.0 is installed (it will show up as MSXML6). Check for it in Add or Remove Programs, and if it is not installed, search for it on microsoft.com, then download it.


Now, on to why we are here...


To create a web-enabled InfoPath form with managed code:
--------------------------------------------------------
1. Create the form in InfoPath. I won't go in detail here. Just create a form with a few fields. Call one of the fields AccountId.

2. change compatibility settings to make it web-enabled:
Tools -> Form Options -> Compatibility -> check Design a form template that can be opened in a browser or InfoPath

3. give the form Full Trust:
Tools -> Form Options -> Security and Trust -> select Full Trust

4. add code - to have custom code when the form loads:
a. set location where you want the Visual Studio project to be created
Tools -> Programming -> under Programming Language, specify language and project location
b. indicate you want to code for the loading of the form
Tools -> Programming -> Loading Event... (it will prompt you to save the form
if you haven't already)
VSTA will create a Visual Studio project (using the form's name) in the location
specified in a. Visual Studio will load with project opened.
c. add code to populate a form field automatically when form loads
under FormEvents_Loading, add code to execute when form loads

Dim strAccountId As String = ""

strAccountId = "P-" & Date.Now.ToString()

'populate account field on infopath form with account #
Me.CreateNavigator.SelectSingleNode("my:MyForm/my:AccountId", Me.NamespaceManager).SetValue(strAccountId )


5. run the project to see the form field being populated


To deploy a web-enabled InfoPath form with managed code:
--------------------------------------------------------
6. publish the form template
back in InfoPath,
a. click Publish Form Template...
b. select To a network location and click Next
c. enter a location to publish the template, a place where an administrator can access and click Next, clear textbox then click Next, click Publish, click Close

7. upload Form Template in Central Admin
a. in Central Administration -> Application Management -> Manage Form Templates, click Upload Form Template
b. browse to location you just published to and select form template
c. click Upload

8. activate the form at the site collection level
In Site Settings -> Site Collection Features, find form template and click Activate
Notice the form template appears as a Site Content Type under Site Settings -> Site Content Types

9. create and set up a form library and add that content type to it
create new form library
a. I go in through View All Site Content -> Create -> Form Library
b. give it a Name
c. Document Template should have Microsoft Office InfoPath form
d. click Create

10. set up library
a. settings -> form library settings -> advanced settings
check Yes for Allow management of content types
check Display as a web page
check No for Display "New Folder" command on the New menu (I just like to remove NewFolder, this isn't really necessary)
click Ok
b. Under Content Types, remove Form and add new content type (form template)
a. click Form -> Delete this content type
b. click Add from existing site content types, select new content type, click Add, then click OK

11. go back to form library and click New

12. You should now see the form display as a web page, and notice the AccountId field was populated programmatically!