Thursday, November 10, 2011

Programmatically get URL to open InfoPath file in browser

If you want to open InfoPath you can not use URL and name to open it(for example: http://spserver/formsite/infopathlibrary/myform.xml) This approach is for saving document and browser shows you save dialog. If you want to open InfoPath in browser you need to change URL quite a bit. Let's have a look like URL look like when InfoPath form is already open in browser: http://spserver/formsite/infopathlibrary/_layouts/FormServer.aspx?XmlLocation=/formsite/infopathlibrary/myform.xml&Source=http%3A%2F%2Fspserver%2FCZ%2Fformsite%2Finfopathlibrary%2FForms%2FAllItems%2Easpx&DefaultItemOpen=1
As you can see now the URL is a bit longer, but still you can simplify it to this:
http://spserver/formsite/_layouts/FormServer.aspx?XmlLocation=formsite/infopathlibrary/myform.xml&DefaultItemOpen=1
This URL also open InfoPath directly in browser. Now lets have a look how code which would generate this URL would look like(code is used in workflow, but for something else would be similar):
string itemUrl = this.workflowProperties.Item.Web.Url;
            itemUrl += "/_layouts/FormServer.aspx?XmlLocation=" + this.workflowProperties.Item.File.ServerRelativeUrl;
            itemUrl += "&DefaultItemOpen=1";
            itemUrl = itemUrl.Replace(" ", "%20");

I guess there is nothing to describe here, maybe just last line with replacing. This line replace all spaces with %20. Reason is simple, some browsers can not convert this unsafe character (I would recommend to create function to convert URL to safe URL) - for better understanding have a look here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

No comments:

Post a Comment