Wednesday, November 7, 2007

IPFullTrust Macro Code Walkthrough

To enable full trust, both the Manifest.xsf and Template.xml files must be modified. The IPFullTrust macro has a Manifest class and a Template class defined with methods and properties to make these modifications. Because Manifest.xsf and Template.xml are both XML files, the two classes inherit from the XmlDocument class in the System.Xml namespace of the .NET Framework Class Library. This facilitates modifications to the files.

The Manifest class contains the method, RemovePublishUrl. Because this class inherits from the XmlDocument class, this method simply needs to call RemoveAttribute, as follows:


Internet 2010The following lines of code in the AddRequireFullTrust method of the Manifest class add the requireFullTrust attribute:

fullTrustAttrib = Me.CreateAttribute("requireFullTrust") fullTrustAttrib.Value = "yes" Me.DocumentElement.Attributes.Append(fullTrustAttrib) 

The solutionName property of the Manifest class sets a valid URN, if one is not already set, as follows:

Try Me.DocumentElement.Attributes("name").Value = Value Catch nrex As System.NullReferenceException 'no name attribute found Me.DocumentElement.Attributes.Append(Me.CreateAttribute("name")) Me.DocumentElement.Attributes("name").Value = Value End Try 

The Template class defines the method, RemoveHrefAttribute. The URL for the form must be removed from Template.xml for a form to be fully trusted. This method parses out the URL from the InfoPath processing instruction and deletes it.

The IPFullTrust macro uses the Solution and Project objects of the Visual Studio .NET automation model to find any InfoPath projects in the current solution, in much the same way as the SignCode macro does:

For Each proj In DTE.Solution.Projects 'iterate through all projects If proj.Kind = IPProjectKind Then 

Once the code finds the InfoPath project, it passes the manifest file name to the constructor of the Manifest class. The class constructor does some basic checking to be sure read/write access to the file is available. If those checks pass, the code can call the object methods and set properties to make the required file modifications:

manifestProjItem = proj.ProjectItems.Item(ManifestFilename) projectManifest = New Manifest(manifestProjItem.FileNames(1)) projectManifest.RemovePublishUrl() projectManifest.AddRequireFullTrust() If Not validUrn(projectManifest.solutionName) Then ' create a valid urn if one is not present. projectManifest.solutionName = "urn:" + proj.Name + ":" + "-myXSD-" + DateTime.Now().ToString("s").Replace(":", "-") End If 

The code then moves on to the Template.xml file. Again, the Template class contains the functionality required to make the necessary changes to the file. The constructor for the Template class also makes the same checks to be sure read/write access is available. Just like the Manifest class, the Template class can call methods and set properties to make the required modifications:

'modify the template file templateProjItem = proj.ProjectItems.Item(TemplateFilename) projectTemplate = New Template(templateProjItem.FileNames(1)) projectTemplate.templateName = projectManifest.solutionName projectTemplate.RemoveHrefAttribute() 

Once the file modifications have been made, the project must be registered on the system. This is the final step in making an InfoPath form fully trusted. InfoPath exposes this functionality. Using COM automation, the RegisterSolution function of the IPFullTrust macro code calls the RegisterSolution method of the InfoPath object model to register the solution:

IPApp = CreateObject("InfoPath.Application") IPApp.RegisterSolution(solutionPath, "overwrite") IPApp.Quit() IPApp = Nothing 

This step completes the process, and your InfoPath project is now fully trusted.

No comments:

Internet Blogosphere