using Microsoft.SharePoint; using Microsoft.SharePoint.Workflow; using System; namespace SPSolutions.SharePoint.Workflow { public class SPWorkflowAssociationAssistant { private SPList _list; private SPListItem _listItem; private SPContentType _contentType; private bool _usingContentTypeTemplate; private SPWorkflowAssociationCollection _workflowAssociationCollection; private SPWorkflowTemplate _workflowTemplate; public SPContentType ContentType { get { return this._contentType; } } public SPList List { get { return this._list; } } public SPListItem ListItem { get { return this._listItem; } } public bool UsingContentTypeTemplate { get { return this._usingContentTypeTemplate; } } public SPWorkflowAssociationCollection WorkflowAssociationCollection { get { return this._workflowAssociationCollection; } } public SPWorkflowTemplate WorkflowTemplate { get { return this._workflowTemplate; } } public SPWorkflowAssociationAssistant(SPWeb web, SPWorkflowAssociationParameters parameters) : this(web, parameters.ListId, parameters.ListItemId, parameters.ContentTypeId, parameters.WorkflowTemplateId, parameters.WorkflowAssociationId) { } public SPWorkflowAssociationAssistant(SPWeb web, Guid listId, int listItemId, SPContentTypeId contentTypeId, Guid workflowTemplateId, Guid workflowAssociationId) { if (listId != Guid.Empty) { this._list = web.Lists[listId]; } if (this._list != null && listItemId != 0) { this._listItem = this._list.GetItemById(listItemId); } if (contentTypeId != SPContentTypeId.Empty) { if (this._list != null) { this._contentType = this._list.ContentTypes[contentTypeId]; } else { this._contentType = web.ContentTypes[contentTypeId]; this._usingContentTypeTemplate = true; } } this._workflowTemplate = web.WorkflowTemplates[workflowTemplateId]; if (this.UsingContentTypeTemplate) { this._workflowAssociationCollection = ((this._contentType == null) ? this._contentType.WorkflowAssociations : null); return; } this._workflowAssociationCollection = ((this.List != null) ? this.List.WorkflowAssociations : null); } public SPWorkflowAssociation CreateWorkflowAssociation(SPWeb web, SPWorkflowAssociationParameters parameters) { if (this.UsingContentTypeTemplate) { return this.CreateContentTypeWorkflowAssociation(web, parameters); } return this.CreateListWorkflowAssociation(web, parameters); } private SPWorkflowAssociation CreateContentTypeWorkflowAssociation(SPWeb web, SPWorkflowAssociationParameters parameters) { return SPWorkflowAssociation.CreateSiteContentTypeAssociation(this.WorkflowTemplate, parameters.WorkflowName, parameters.TaskList, parameters.HistoryList); } private SPWorkflowAssociation CreateListWorkflowAssociation(SPWeb web, SPWorkflowAssociationParameters parameters) { if (parameters.IsNewTaskList && SPListUtil.DoesListExist(web, parameters.TaskListName)) { throw new SPException("A list already exists with the same name as that proposed for the new task list. Use your browser's Back button and either change the name of the workflow or select an existing task list.<br>"); } if (parameters.IsNewHistoryList && SPListUtil.DoesListExist(web, parameters.HistoryListName)) { throw new SPException("A list already exists with the same name as that proposed for the new history list. Use your browser's Back button and either change the name of the workflow or select an existing history list.<br>"); } Guid guid; if (parameters.IsNewTaskList) { string text = string.Format("Task list for the {0} workflow.", parameters.WorkflowName); guid = web.Lists.Add(parameters.TaskListName, text,SPListTemplateType.Tasks); } else { guid = parameters.TaskListId; } Guid guid2; if (parameters.IsNewHistoryList) { string text = string.Format("History list for the {0} workflow.", parameters.WorkflowName); guid2 = web.Lists.Add(parameters.HistoryListName, text, SPListTemplateType.WorkflowHistory); } else { guid2 = parameters.HistoryListId; } SPList sPList = web.Lists[guid]; SPList sPList2 = web.Lists[guid2]; return SPWorkflowAssociation.CreateListAssociation(this.WorkflowTemplate, parameters.WorkflowName, sPList, sPList2); } public SPWorkflowAssociation GetOrCreateWorkflowAssocation(SPWeb web, SPWorkflowAssociationParameters parameters) { if (parameters.IsNewWorkflowAssociation) { return this.CreateWorkflowAssociation(web, parameters); } if (parameters.WorkflowAssociationId == Guid.Empty) { throw new ApplicationException("Invalid Workflow Association ID."); } return this.WorkflowAssociationCollection[parameters.WorkflowAssociationId]; } public static string GetQueryString(SPWorkflowAssociationParameters parameters) { string text; if (parameters.ContentTypeId != SPContentTypeId.Empty) { text = string.Format("ctype={1}", parameters.ContentTypeId); if (parameters.ListId != Guid.Empty) { text += string.Format("&List={0}", parameters.ListId); } } else { text = string.Format("List={0}", parameters.ListId); } return text; } public void SaveWorkflowAssociation(SPWorkflowAssociation workflowAssociation, SPWorkflowAssociationParameters parameters) { if (this.UsingContentTypeTemplate) { if (parameters.IsNewWorkflowAssociation) { this.ContentType.AddWorkflowAssociation(workflowAssociation); } else { this.ContentType.UpdateWorkflowAssociation(workflowAssociation); } if (parameters.UpdateLists) { this.ContentType.UpdateWorkflowAssociationsOnChildren(false); return; } } else { if (parameters.IsNewWorkflowAssociation) { this.List.AddWorkflowAssociation(workflowAssociation); } else { this.List.UpdateWorkflowAssociation(workflowAssociation); } if (workflowAssociation.AllowManual && this.List.EnableMinorVersions) { if (this.List.DefaultContentApprovalWorkflowId != workflowAssociation.Id && parameters.SetDefault) { if (!this.List.EnableModeration) { this.List.EnableModeration=(true); } this.List.DefaultContentApprovalWorkflowId=(workflowAssociation.Id); this.List.Update(); return; } if (this.List.DefaultContentApprovalWorkflowId == workflowAssociation.Id && !parameters.SetDefault) { this.List.DefaultContentApprovalWorkflowId=(Guid.Empty); this.List.Update(); } } } } } }