Undefined responseXML on StartWorkflow

May 21, 2013 at 9:43 PM
I am trying to run simple workflow via SPServices and cannot get anything to return properly. I am getting an undefined xData.responseXML. Anyone able to point me in correct direction?
function RunWorkflow(ItemURL, ItemID, WorkflowID)  {
        alert("attempt wf");
        var loadingImage = 'Loader' + ItemID;
        var workflowDiv = 'WorkflowDiv' + ItemID;
        //Show our loading image
        //$(loadingImage).show();
        document.getElementById(loadingImage).style.visibility = 'visible';
        $().SPServices({
            operation: "StartWorkflow",
            item: ItemURL,
            debug: true,
            templateId: WorkflowID, 
            workflowParameters: "<root />",
            completefunc: function(xData, Status) {
                //$(workflowDiv).html("Item Emailed");
                if(Status != "error"){
                    document.getElementById(workflowDiv).innerHTML = 'Item emailed';
                    window.location.href = window.location.href;// execute page reload without confirmations from user
                }
                else{
                    document.getElementById(workflowDiv).innerHTML = '<div style=\"color:#FF0000\">Error</div>';
                    if (xData.responseXML != undefined)
                        window.alert(xData.responseXML.text);
                    else
                        window.alert("Error. call assistance, probably the workflow was changed and has another GUID.");
                }

            }
        });
    }
Coordinator
May 21, 2013 at 9:49 PM
Have you looked at the network traffic with Firebug or Fiddler? I'd also strip things down to the simplest possible call to make sure I didn't have a missing comma or something.

M.
May 22, 2013 at 3:24 PM
Here is the dataview side of things that launches jQuery and SPServices (obviously the RequestListName is replaced with the actual list name):
<a href="#">
<xsl:attribute name="onclick">
<xsl:text>javascript:StartWorkflow('</xsl:text>
<xsl:value-of select="@EncodedAbsUrl" disable-output-escaping="yes"/>
<xsl:text>','</xsl:text>
<xsl:value-of select="@ID" disable-output-escaping="yes"/>
<xsl:text>','RequestListName','emailTest');</xsl:text>
</xsl:attribute>
Email Me</a>
My javascript function is pulling in all parameters as string objects, but I am not sure if that causes issues or not with the SPServices functions. And I slightly modified my script but I am still getting an undefined error on xData.responseXML in the RunWorkflow function:
function StartWorkflow(ItemURL, ItemID, ListName, DesiredWorkflow)  {
        var id = "";
        $().SPServices({
            operation: "GetList",  
            listName: "RequestListName",  
            async: false,  
            completefunc: function (xData, Status) {  
                id = $(xData.responseXML).find("List").attr("ID").toString();  
            }  
        });
        //Get list Id from current page 
        if (id) { 
            this.guid = new SP.Guid(id); 
            var context = SP.ClientContext.get_current(); 
            var lists = context.get_web().get_lists(); 
            var list = lists.getById(this.guid); 
            //Set workflows variable to load and be accessible in callbacks 
            this.workflows = list.get_workflowAssociations(); 
            context.load(this.workflows); 
            context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed)); 
        } 
        function onQuerySucceeded(sender, args) { 
            var enumerator = this.workflows.getEnumerator(); 
            while (enumerator.moveNext()) { 
                var workflow = enumerator.get_current();
                if (workflow.get_enabled() && workflow.get_allowManual()) { 
                    var workflowName = workflow.get_name(); 
                    if (workflowName == DesiredWorkflow){
                        var workflowGuid = '{' + workflow.get_id().toString() + '}';
                        //alert(workflowName + "  .....  " + workflowGuid);
                        RunWorkflow(ItemURL, ItemID, workflowGuid);
                    }
                }
            } 
        } 
        function onQueryFailed(sender, args) { 
            alert('Unable to retrieve workflows: ' + args.get_message()); 
        } 
    }
    function RunWorkflow(ItemURL, ItemID, workflowGuid)  {
        var loadingImage = 'Loader' + ItemID;
        var workflowDiv = 'WorkflowDiv' + ItemID;
        var workflowParams = "<root />";
        //Show our loading image
        document.getElementById(loadingImage).style.visibility = 'visible';
        $().SPServices({
            operation: "StartWorkflow",
            async:true,
            item: ItemURL,
            templateId: workflowGuid, 
            workflowParameters: workflowParams,
            completefunc: function (xData, Status) { 
                if(Status != "error"){
                    document.getElementById(workflowDiv).innerHTML = 'Item emailed';
                }
                else{
                    document.getElementById(workflowDiv).innerHTML = '<div style=\"color:#FF0000\">Error</div>';
                    if (xData.responseXML != undefined)
                        window.alert(xData.responseXML.text);
                    else
                        window.alert("Error. call assistance, probably the workflow was changed and has another GUID.");            }

            }
        });
    }
When I use the Fiddler I get a 404 error when it hits the SPServices function for StartWorkflow (GetList works fine). This is what is confusing me. I know SPServices is working because it works for GetList. But the StartWorkflow function is failing.

Again, any help to clear up my confusion on SPServices here would be appreciated.
May 22, 2013 at 3:34 PM
Does it have anything to do with encoded URL?

The following is being passed from the dataview to the jQuery functions:
https://exampleSite/subsite/Lists/listName/1997_.000
May 23, 2013 at 3:05 PM
Edited May 23, 2013 at 3:18 PM
In scenarios where I needed to have the ability to start a workflow via a hyperlink, I daisy-chained 2 functions, GetTemplatesForItem and StartWorkflow. GetTemplatesForItem gives you the proper GUID of the workflow you wish to start and then you pass that data along into the StartWorkflow function. In the second link, note the proper format the URL needs to be in.

So to trigger it on my hyperlink click, I'd just pass the ID of the item into my function, build the proper URL, find the workflow GUID, then pass the GUID and item URL into the start workflow function.

You can do this all in jQuery too, no need to use the client object model.

Here is the link to how I did it.