简体   繁体   中英

How to reset digital signature of an InfoPath form during c# workflow in Sharepoint 2010?

As the question says, I have an InfoPath form running on SP2010 using ac# workflow upon submission. If the form is rejected during workflow, then I need to reset it. I have everything under control, EXCEPT how to reset digital signatures to null, nill, nada, nothing, non-extant! Any ideas? I'm looking at Google now, but at current, I'm not even sure of an om for digital signatures?

Wow, i notice this question suddenly gaining alot of pop with bounty almost gone. Just putting it out there, I did not intend to not bounty someone, but i needed the answer earlier this week (2nd week Nov 2012) and thus i searched and played and teetered with code as much as possible till i ended up finding my own answer before anyone else answered me. However, for future reference, if someone gives a better answer, i'll gladly come back and rep them. Thank you all for the support and I really hope my answer is as useful to another as it was to me.

NOW Bloggered && Gisted May no one ever again have to search as hard as I did for this answer, :P

¡¡¡ I FOUNDMYFRIGGIN ' ANSWER ! ! !

¡¡¡ And it works from the workflow !!!

Through much trial and tribulation I was finally able to come up with a solution. It involves a few steps. One, elevate security! Otherwise, non-admin users will cause the workflow to error. Seems like it should work this way, but ... Secondly, get the right schema! It took me a while to find mine, i forgot the exact steps, but, it's not hard to find. UPDATED: Can be found as an attribute of xmlDoc.Document, see updated code Step through (debug) your workflow, without the namespace/schema and highlight your document when it gets to it. One of the properties is an url that is the schema link. Anyway, you wanna see the solution!? Do ya? Look down!

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPFile formFile = workflowProperties.Item.File;
    MemoryStream ms = new MemoryStream(formFile.OpenBinary());
    XmlTextReader rdr = new XmlTextReader(ms);
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(rdr);
    rdr.Close();
    ms.Close();
    XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
    String schemaUri = xmlDoc.DocumentElement.GetAttributeNode("xmlns:my") != null ? xmlDoc.DocumentElement.GetAttributeNode("xmlns:my").Value : "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-09-04T20:19:31";
    nsm.AddNamespace("my", schemaUri);
    XmlNode nodeSignatureCollection = xmlDoc.DocumentElement.SelectSingleNode("my:signatures1", nsm);
    if (nodeSignatureCollection != null)
    {
        if (nodeSignatureCollection.HasChildNodes)
        {
            foreach (XmlNode nodeSignature in nodeSignatureCollection.ChildNodes)
            {
                //  HERE IT IS!!!
                if (nodeSignature.HasChildNodes && !nodeSignature.IsReadOnly) nodeSignature.RemoveAll();
            }
        }
    }
    byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
    formFile.SaveBinary(xmlData);
    formFile.Update();
});

Keep in mind, this setup is for going through multiple signatures. Although I doubt anything would change if there was only one signature.

Any suggestions on making this sweeter and smaller are accepted, however, I must request an explanation. Honestly, I barely understand what is going on here!

The following answer only HALF works. It is left here for instructional purpose. ( Full working answer can be found here. ) It works for admin users, but nothing less. It also only works from the InfoPath form behind code. NOT from the workflow. Adding elevated privlage seems to have 0 effect

I'm leaving this answer up here along with my other so that someone may learn from both examples or possibly even instruct others (including myself) via comments and what not on why one way may be better than the other. At this point, I really don't care to explain anymore as I really don't care to see any of this code ever again! LoL!

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
    string[] actionFields = new string[] { "/my:myFields/my:.../my:...", "/my:myFields/my:.../my:...", etc... };
    for (int i = 0; i < actionFields.Length; i++)
    {
        String field = actionFields[i];
        XPathNavigator node = this.MainDataSource.CreateNavigator().SelectSingleNode(field, this.NamespaceManager);
        if (node.Value.ToLower() == "reject")
        {
            XPathNavigator sigNode = this.MainDataSource.CreateNavigator();
            if (this.Signed) //then unsign it
            {
                for (int ii = 2; ii <= 13; ii++)
                {
                    try
                    {
                        XPathNavigator xSignedSection = sigNode.SelectSingleNode(String.Format("my:myFields/my:signatures1/my:signatures{0}", ii), this.NamespaceManager);
                        if (xSignedSection.HasChildren)
                        {
                            xSignedSection.MoveToChild(XPathNodeType.Element); xSignedSection.DeleteSelf();
                        };
                    }
                    catch (Exception ex) { };
                };
            };
        };
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM