Update a Infopath request form from Sharepoint Workflow

Posted on August 19, 2010. Filed under: Code Snippets | Tags: , , , , |

Use the below method in SharePoint workflow to modify a field value in your request form.

private void updateInfoPathRequestForm(string xpathNode, string nodeValue)
        {
            SPUserToken token = workflowProperties.Web.CurrentUser.UserToken;
            using (SPSite _site = new SPSite(workflowProperties.Site.ID, token))
            {
                _site.AllowUnsafeUpdates = true;
                using (SPWeb _web = _site.OpenWeb(workflowProperties.Web.ID))
                {
                    SPListItem item = _web.Lists[workflowProperties.List.ID].Items[workflowProperties.Item.UniqueId];
                    string checkinComment = string.Empty;
                    _web.AllowUnsafeUpdates = true;
                    try
                    {
                        if (item.File.CheckOutStatus != SPFile.SPCheckOutStatus.None) { item.File.UndoCheckOut(); }
                        item.File.CheckOut();

                        MemoryStream myInStream = new MemoryStream(item.File.OpenBinary(), true);
                        XmlDocument doc = new XmlDocument();

                        doc.Load(myInStream);
                        myInStream.Close();
                        XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(doc.NameTable);
                        XmlElement root = doc.DocumentElement;
                        nameSpaceManager.AddNamespace(“my”, root.NamespaceURI);

                        XmlNode nodeNav = root.SelectSingleNode(xpathNode, nameSpaceManager);
                        nodeNav.InnerText = @nodeValue;

                        checkinComment = “Workflow Edits”;
                        Stream myOutStream = new MemoryStream(Encoding.Utf8.GetBytes(doc.OuterXml), true);
                        item.File.SaveBinary(myOutStream, true);
                        myOutStream.Close();
                    }
                    catch (Exception ex)
                    {
                        //Write the Exception to the Portal Log.
                        PortalLog.LogString(“xxxxxxRequestWorkflow-Exception Occured on custom method-updateInfoPathRequestForm. Exception: {0} || {1}”, ex.Message, ex.StackTrace);
                        
                        //throw new SPException(“”);
                    }
                    finally
                    {
                        item.File.CheckIn(checkinComment);
                    }
                    _web.AllowUnsafeUpdates = false;
                }
                _site.AllowUnsafeUpdates = false;
            }
        }

The only issues i would assume would be the encoding styles while you are writing back the stream to the form, which may effect some out of date character set or lang pack character set

Read Full Post | Make a Comment ( None so far )

    About

    Xperiments n Xperiences. For the Last 5 years I have been providing variuos portal solutions (SharePoint focused) to my clients and now I intend to publish few of those solutions 'Not As It Is', which might help you. – Mash

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS

    Meta

Liked it here?
Why not try sites on the blogroll...