public static WorkflowClass.WorkflowInfo GetItemInfo(ClientContext ctx, ListItem item) { WorkflowClass.WorkflowInfo WorkflowInfo; WorkflowInfo.stages = 0; WorkflowInfo.stage1group = ""; WorkflowInfo.stage2group = ""; WorkflowInfo.stage3group = ""; WorkflowInfo.stage4group = ""; ctx.Load(item.ParentList.WorkflowAssociations); ctx.ExecuteQuery(); WorkflowAssociationCollection wfs = item.ParentList.WorkflowAssociations; foreach (var wf in wfs) { if (wf.InternalName.Equals("Item Publication")) { string associationData = wf.AssociationData; WorkflowInfo.stages = getStages(associationData); WorkflowInfo.stage2group = getApprovers(associationData); WorkflowInfo.stage4group = getPublishers(associationData); } } return(WorkflowInfo); }
private void button1_Click(object sender, EventArgs e) { WorkflowAssociationCollection wfs = ctx.Web.WorkflowAssociations; ctx.Load(wfs); ctx.ExecuteQuery(); foreach(WorkflowAssociation wf in wfs) { System.Diagnostics.Trace.WriteLine(wf.Name); } ListCollection lsts = ctx.Web.Lists; ctx.Load(lsts, l => l.Include(lst => lst.Title, lst => lst.WorkflowAssociations)); ctx.ExecuteQuery(); foreach (List lst in lsts) { wfs = lst.WorkflowAssociations; ctx.Load(wfs); ctx.ExecuteQuery(); if (lst.WorkflowAssociations.Count > 0) { foreach(WorkflowAssociation wf in lst.WorkflowAssociations) { System.Diagnostics.Trace.WriteLine(wf.Name); } } } }
public static bool TriggerWorkflow(string SiteURL, string WorkflowName) { bool status = false; try { string siteUrl = SiteURL; string userName = "******"; string password = "******"; //Name of the SharePoint 2010 Workflow to start. string workflowName = WorkflowName; using (ClientContext clientContext = new ClientContext(siteUrl)) { SecureString securePassword = new SecureString(); foreach (char c in password.ToCharArray()) { securePassword.AppendChar(c); } clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword); Web web = clientContext.Web; //Workflow Services Manager which will handle all the workflow interaction. WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web); //Will return all Workflow Associations which are running on the SharePoint 2010 Engine WorkflowAssociationCollection wfAssociations = web.WorkflowAssociations; //Get the required Workflow Association WorkflowAssociation wfAssociation = wfAssociations.GetByName(workflowName); clientContext.Load(wfAssociation); clientContext.ExecuteQuery(); //Get the instance of the Interop Service which will be used to create an instance of the Workflow InteropService workflowInteropService = wfServicesManager.GetWorkflowInteropService(); var initiationData = new Dictionary <string, object>(); //Start the Workflow ClientResult <Guid> resultGuid = workflowInteropService.StartWorkflow(wfAssociation.Name, new Guid(), Guid.Empty, Guid.Empty, initiationData); clientContext.ExecuteQuery(); status = true; } } catch (Exception ex) { status = false; throw; } return(status); }
/// <summary> /// Returns all login-names of given Users as HashSet. /// </summary> /// <param name="workflowAssociationCollection">the collection of WorkflowAssociatoin</param> /// <returns>login-names of Users</returns> internal static HashSet <string> GetAllNames(this WorkflowAssociationCollection workflowAssociationCollection) { HashSet <string> names = new HashSet <string>(); foreach (var workflowAssociation in workflowAssociationCollection) { names.Add(workflowAssociation.Name); } return(names); }
/// <summary> /// Returns all WorkflowAssociationCollection of the given SharePoint. /// </summary> /// <param name="clientContext">ClientContext of SharePoint</param> /// <returns>WorkflowAssociationCollection of given SharePoint</returns> /// <exception cref="ElementsMigrationException">if fetching WorkflowAssociationCollection fails</exception> private WorkflowAssociationCollection GetAllWorkflowAssociationCollection(ClientContext clientContext) { Web web = clientContext.Web; WorkflowAssociationCollection collection = web.WorkflowAssociations; try { clientContext.Load(collection); clientContext.ExecuteQuery(); } catch (Exception e) { Console.WriteLine("Exception during fetching the WorkflowAssociations.", e); throw new ElementsMigrationException("Exception during fetching the WorkflowAssociations.", e); } return(collection); }
private void btnLoadForm_Click(object sender, EventArgs e) { // ===================== Populate Date Generated ============================== Word.ContentControl ccDateGenerated = GetContentControl("txtDateGenerated"); ccDateGenerated.Range.Text = DateTime.Now.ToString("MM/dd/yyyy"); String Stage = "Page Load"; WebCollection childWebs = null; String txtURL = String.Empty; Word.ContentControl ccURL = GetContentControl("txtURL"); // Clear errors SetContentControl("txtSmartArtError", ""); SetContentControl("txtURLError", ""); if (ccURL != null) { Stage = "Load Action Pane"; // http://oigportal.hhsoig.gov/sites/OAS/AATS/TMSIS txtURL = ccURL.Range.Text; txtURL = txtURL.Replace("SitePages/Home.aspx", ""); txtURL = txtURL.TrimEnd('/'); actionPane.URL = txtURL; actionPane.LoadControl(); try { Stage = "Load Web"; ClientContext clientContext = new ClientContext(txtURL); Web currentWeb = clientContext.Web; clientContext.Load(currentWeb); clientContext.ExecuteQuery(); // Get Web details Guid webId = currentWeb.Id; SetContentControl("txtTitle", currentWeb.Title); SetContentControl("txtCreated", currentWeb.Created.ToString("MM/dd/yyyy")); SetContentControl("txtModified", currentWeb.LastItemModifiedDate.ToString("MM/dd/yyyy")); long webSize = GetWebSize(currentWeb); SetContentControl("txtSize", webSize.ToString("N0")); // Set document properties Microsoft.Office.Core.DocumentProperties properties; properties = (Office.DocumentProperties) this.CustomDocumentProperties; // properties["Title"].Value = currentWeb.Title; #region Smart Art Population // ============ Smart Art ===================================================== try { // Set up for diagram Stage = "Modify Smart Art"; // for background color of the current web cell in the smartart. const int OrangeCell = unchecked ((int)0xED7D31); Site tempSite = clientContext.Site; clientContext.Load(tempSite); clientContext.ExecuteQuery(); string siteUrl = tempSite.Url + "/"; // http://oigportal.hhsoig.gov/sites/OAS Web tmpRoot = tempSite.RootWeb; clientContext.Load(tmpRoot); clientContext.ExecuteQuery(); string rootTitle = tmpRoot.Title; // Get site names by breaking down URL. // SharePoint 2010 client Web class doesn't have any way to get the parent web. // example: AATS/TMSIS string navTree = txtURL.Replace(siteUrl, ""); string[] nodes = navTree.Split('/'); // Find the diagram and get a reference to it. Word.InlineShape treeShape = null; foreach (Word.InlineShape tmpShape in this.InlineShapes) { if (tmpShape.Type == Word.WdInlineShapeType.wdInlineShapeSmartArt) { treeShape = tmpShape; } } Office.SmartArt treeArt = treeShape.SmartArt; // treeShape.Height // clear out existing nodes foreach (Office.SmartArtNode tmpNode in treeArt.Nodes) { if (tmpNode != null) { tmpNode.Delete(); } } Office.SmartArtNode rootNode = treeArt.Nodes.Add(); rootNode.TextFrame2.TextRange.Text = rootTitle; // Nodes from root to current site foreach (string tmpNodeText in nodes) { Office.SmartArtNode tmpChildNode = treeArt.Nodes.Add(); tmpChildNode.TextFrame2.TextRange.Text = tmpNodeText; } // Root node - add then node, then set the text. Office.SmartArtNode currentNode = treeArt.Nodes[treeArt.Nodes.Count]; currentNode.TextFrame2.TextRange.Text = currentWeb.Title; // set root node color currentNode.Shapes.Fill.ForeColor.RGB = 0xED7D31; // OrangeCell; // Child webs for SmartArt childWebs = currentWeb.Webs; clientContext.Load(childWebs); clientContext.ExecuteQuery(); foreach (Web tmpWeb in childWebs) { Office.SmartArtNode childNode = currentNode.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow); childNode.TextFrame2.TextRange.Text = tmpWeb.Title; } } catch (Exception ex) { Word.ContentControl smartArtError = GetContentControl("txtSmartArtError"); Word.Range tagRange = smartArtError.Range; tagRange.Text = String.Concat("ERROR: ", ex.Message); tagRange.Font.Color = Word.WdColor.wdColorRed; } #endregion #region Build Child Web Table // ============ Child Web Table =============================================== Stage = "Load Child Web Table"; Word.Table webTable = GetTable("ChildWebs"); if (webTable != null) { foreach (Web tmpWeb in childWebs) { Word.Row newRow = webTable.Rows.Add(); newRow.Cells[1].Range.Text = tmpWeb.Title; newRow.Cells[2].Range.Text = tmpWeb.ServerRelativeUrl; // newRow.Cells[3].Range.Text = Owners newRow.Cells[4].Range.Text = tmpWeb.Created.ToString("MM/dd/yyyy"); long WebSize = GetWebSize(tmpWeb); newRow.Cells[5].Range.Text = WebSize.ToString("N0"); } } #endregion #region Build Child Object Table // ================== Child Object Table ========================================= Microsoft.SharePoint.Client.ListCollection webLists = currentWeb.Lists; clientContext.Load(webLists); clientContext.ExecuteQuery(); Word.Table objTable = GetTable("tblContentObjects"); if (objTable != null) { foreach (List tmpList in webLists) { Word.Row newRow = objTable.Rows.Add(); newRow.Cells[1].Range.Text = tmpList.BaseType.ToString(); newRow.Cells[2].Range.Text = tmpList.Title; newRow.Cells[3].Range.Text = tmpList.ItemCount.ToString(); newRow.Cells[4].Range.Text = tmpList.LastItemModifiedDate.ToString("MM/dd/yyyy"); } } #endregion #region Build Permissions Table // =================== Permissions Table ============================================== Stage = "Load Permissions Table"; Word.Table permTable = GetTable("tblPermissions"); RoleAssignmentCollection roleAssignments = currentWeb.RoleAssignments; clientContext.Load(roleAssignments); clientContext.ExecuteQuery(); Stage = "Role Assignments"; foreach (RoleAssignment assign in roleAssignments) { clientContext.Load(assign); clientContext.ExecuteQuery(); Stage = "Load Role Principal"; Principal tmpMember = assign.Member; clientContext.Load(tmpMember); clientContext.ExecuteQuery(); Word.Row newRow = permTable.Rows.Add(); newRow.Cells[1].Range.Text = assign.Member.Title; newRow.Cells[2].Range.Text = assign.Member.PrincipalType.ToString(); newRow.Cells[3].Range.Text = assign.Member.LoginName; Stage = "Role Collection"; RoleDefinitionBindingCollection roles = assign.RoleDefinitionBindings; clientContext.Load(roles); clientContext.ExecuteQuery(); Stage = "Role Definitions"; foreach (RoleDefinition roleDef in roles) { clientContext.Load(roleDef); clientContext.ExecuteQuery(); switch (roleDef.Name) { case "Full Control": newRow.Cells[4].Range.Text = "X"; break; case "Design": newRow.Cells[5].Range.Text = "X"; break; case "Contribute": newRow.Cells[6].Range.Text = "X"; break; case "Read": newRow.Cells[7].Range.Text = "X"; break; } } } #endregion #region Fill Workflow Table Stage = "Load Workflow Table"; Word.Table workflowTable = GetTable("tblWorkflows"); WorkflowAssociationCollection workflows = currentWeb.WorkflowAssociations; clientContext.Load(workflows); clientContext.ExecuteQuery(); foreach (WorkflowAssociation workflow in workflows) { clientContext.Load(workflow); clientContext.ExecuteQuery(); Word.Row newRow = workflowTable.Rows.Add(); newRow.Cells[1].Range.Text = workflow.Name; } #endregion } catch (Exception ex) { Word.ContentControl urlError = GetContentControl("txtURLError"); Word.Range rngError = urlError.Range; rngError.Text = String.Concat("ERROR at stage ", Stage, ": ", ex.Message); rngError.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed; } } }
/// <summary> /// Imports new workflows to the target SharePoint. /// </summary> /// <exception cref="ElementsMigrationException">if migration fails</exception> private void ImportNewWorkflow() { Console.WriteLine("Import new WorkflowAssociations"); WorkflowAssociationCollection sourceWorkflowAssociations = this.GetAllWorkflowAssociationCollection(this.SourceClientContext); WorkflowAssociationCollection targetWorkflowAssociations = this.GetAllWorkflowAssociationCollection(this.TargetClientContext); if (sourceWorkflowAssociations.Count == 0) { Logger.AddMessage("no workflows to migrate..."); return; } HashSet <string> targetWorkflowNames = targetWorkflowAssociations.GetAllNames(); foreach (var sourceWorkflowAssociation in sourceWorkflowAssociations) { if (!targetWorkflowNames.Contains(sourceWorkflowAssociation.Name)) { Console.WriteLine("import new workflow '{0}'", sourceWorkflowAssociation.Name); Logger.AddMessage("import new workflow '" + sourceWorkflowAssociation.Name + "'"); WorkflowAssociationCreationInformation creationObject = new WorkflowAssociationCreationInformation(); creationObject.Name = sourceWorkflowAssociation.Name; creationObject.HistoryList = this.GetHistoryList(this.SourceClientContext); creationObject.TaskList = this.GetTaskList(this.SourceClientContext); creationObject.Template = this.GetTemplate(); WorkflowAssociation targetWorkflowAssociation = targetWorkflowAssociations.Add(creationObject); try { targetWorkflowAssociation.AllowManual = sourceWorkflowAssociation.AllowManual; } catch (PropertyOrFieldNotInitializedException) { } try { targetWorkflowAssociation.AutoStartChange = sourceWorkflowAssociation.AutoStartChange; } catch (PropertyOrFieldNotInitializedException) { } try { targetWorkflowAssociation.AutoStartCreate = sourceWorkflowAssociation.AutoStartCreate; } catch (PropertyOrFieldNotInitializedException) { } try { targetWorkflowAssociation.Enabled = sourceWorkflowAssociation.Enabled; } catch (PropertyOrFieldNotInitializedException) { } try { targetWorkflowAssociation.AssociationData = sourceWorkflowAssociation.AssociationData; } catch (PropertyOrFieldNotInitializedException) { } } else { Console.WriteLine("don't have to migrate workflow '{0}'", sourceWorkflowAssociation.Name); Logger.AddMessage("don't have to migrate workflow '" + sourceWorkflowAssociation.Name + "'"); } } try { this.TargetClientContext.ExecuteQuery(); } catch (Exception e) { Console.WriteLine("Exception during importing new Workflows.", e); Logger.AddMessage("Exception during importing new Workflows. Error = " + e.Message); throw new ElementsMigrationException("Exception during importing new Workflows.", e); } }
private static void StartWFOnPremise() { //Site Details string siteUrl = "http://rdits-sp13-dev2/sites/gs/"; string userName = "******"; string password = "******"; //Name of the SharePoint2010 List Workflow string workflowName = "Approbation 2010"; //Name of the List to which the Workflow is Associated string targetListName = "cadeaux"; //Guid of the List to which the Workflow is Associated Guid targetListGUID = new Guid("cb1ef47f-6fbf-4119-97c5-057006db6a13"); //Guid of the ListItem on which to start the Workflow //Guid targetItemGUID = new Guid("B10F6CF0-86F2-4F6D-B982-BBF9FB38897E"); Guid targetItemGUID = new Guid("31D3B801-5C0C-4758-8AE0-3BF23BD6E430"); using (ClientContext clientContext = new ClientContext(siteUrl)) { //SecureString securePassword = new SecureString(); //foreach (char c in password.ToCharArray()) securePassword.AppendChar(c); //clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword); Web web = clientContext.Web; List list = web.Lists.GetByTitle(targetListName); ListItem item = list.GetItemById(19); clientContext.Load(item); clientContext.ExecuteQuery(); string guid = item["GUID"].ToString(); //Workflow Services Manager which will handle all the workflow interaction. WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web); //Will return all Workflow Associations which are running on the SharePoint 2010 Engine WorkflowAssociationCollection wfAssociations = web.Lists.GetByTitle(targetListName).WorkflowAssociations; //Get the required Workflow Association WorkflowAssociation wfAssociation = wfAssociations.GetByName(workflowName); clientContext.Load(wfAssociation); clientContext.ExecuteQuery(); //Get the instance of the Interop Service which will be used to create an instance of the Workflow InteropService workflowInteropService = wfServicesManager.GetWorkflowInteropService(); var initiationData = new Dictionary <string, object>(); initiationData.Add("DisplayName", "Julien Bessiere"); initiationData.Add("AccountId", "i:0#.f|membership|[email protected]"); initiationData.Add("AccountType", "User"); //Start the Workflow ClientResult <Guid> resultGuid = workflowInteropService.StartWorkflow(wfAssociation.Name, new Guid(), targetListGUID, Guid.Parse(guid), initiationData); try { clientContext.ExecuteQuery(); } catch (Exception ex) { throw; } } }
private static void StartWFOnLine() { //Site Details string siteUrl = "https://jbes.sharepoint.com/sites/gosport"; string userName = "******"; string password = "******"; //Name of the SharePoint2010 List Workflow string workflowName = "Approbation 2010"; //Name of the List to which the Workflow is Associated string targetListName = "Liste des demandes"; //Guid of the List to which the Workflow is Associated Guid targetListGUID = new Guid("cbe00484-055e-4dca-ade1-863ef7b202ba"); //Guid of the ListItem on which to start the Workflow //Guid targetItemGUID = new Guid(""); using (ClientContext clientContext = new ClientContext(siteUrl)) { SecureString securePassword = new SecureString(); foreach (char c in password.ToCharArray()) { securePassword.AppendChar(c); } clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword); Web web = clientContext.Web; List list = web.Lists.GetByTitle(targetListName); ListItem item = list.GetItemById(1); clientContext.Load(item); clientContext.ExecuteQuery(); string guid = item["GUID"].ToString(); //Workflow Services Manager which will handle all the workflow interaction. WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web); //Will return all Workflow Associations which are running on the SharePoint 2010 Engine WorkflowAssociationCollection wfAssociations = list.WorkflowAssociations; //Get the required Workflow Association WorkflowAssociation wfAssociation = wfAssociations.GetByName(workflowName); clientContext.Load(wfAssociation); clientContext.ExecuteQuery(); //Get the instance of the Interop Service which will be used to create an instance of the Workflow InteropService workflowInteropService = wfServicesManager.GetWorkflowInteropService(); var initiationData = new Dictionary <string, object>(); //Start the Workflow ClientResult <Guid> resultGuid = workflowInteropService.StartWorkflow(wfAssociation.Name, new Guid(), targetListGUID, Guid.Parse(guid), initiationData); clientContext.ExecuteQuery(); } }