2 Ways to retrieve the SharePoint site collection URL of an InfoPath browser form

Learn how you can write C# code in an InfoPath browser form to be able to retrieve the URL of the SharePoint site collection that the InfoPath form is part of.

ADVERTISEMENTS

On some ocassions you may want to access SharePoint objects such as lists and libraries that are located in the same SharePoint site collection as where an InfoPath browser form is located.

You can use one of the following two methods to find and retrieve the URL of the SharePoint site collection an InfoPath browser form is located in from within the code-behind of the InfoPath form.

Method 1: Use the SPContext object

The following code makes use of an SPContext object to retrieve the URL of the SharePoint site collection of an InfoPath browser form and store it in a text field named url on the InfoPath form:

// Get the current SharePoint context
SPContext ctx = SPContext.Current;

if (ctx != null)
{
  // Retrieve the URL of the SharePoint site collection
  SPSite site = ctx.Site;
  string siteUrl = site.Url;
  MainDataSource.CreateNavigator().SelectSingleNode(
    "//my:url", NamespaceManager).SetValue(siteUrl);
}

Note: You must add a reference to the SharePoint DLL and a using statement for Microsoft.SharePoint for the code to work.

Method 2: Use the Source parameter

You can use the Source parameter on an InfoPath browser form to determine which SharePoint site collection the InfoPath form is located in.

The MSDN documentation gives the following description for the Source parameter:

The location to which the user will be redirected when the form is closed. The URL must be in the same site collection or an error will be returned.

You can use the same principles as described in Submitting to ‘this’ document library to retrieve and use the value of the Source parameter in the code-behind of an InfoPath browser form.

Create the following FormState dictionary object in the FormCode class:

private object sourceParam
{
  get { return FormState["sourceParam"]; }
  set { FormState["sourceParam"] = value; }
}

Add a Form Loading event to the InfoPath form template and then add the following code to it:

if (Application.Environment.IsBrowser
  && e.InputParameters.ContainsKey("Source"))
{
  sourceParam = e.InputParameters["Source"];
}

Add the following code in the Clicked event of a button control:

if (sourceParam != null)
{
  UriBuilder ub = new UriBuilder(sourceParam.ToString());
  string siteUrl = ub.Scheme + @"://" + ub.Host;

  MainDataSource.CreateNavigator().SelectSingleNode(
    "//my:url", NamespaceManager).SetValue(siteUrl);
}

Note: The preceding code writes the URL of the top-level site of the SharePoint site collection to a text field named url located on the InfoPath form. The top-level site of the SharePoint site collection is located at http://server in the case above and a subsite at location http://server/site.

The advantage of using the second method over the first is that with the second method you do not have to reference the SharePoint DLL to be able to retrieve the URL of the SharePoint site collection, while a reference to the SharePoint DLL is required in the first method to be able to use the SPContext object.

When working with code in InfoPath browser-compatible form templates, you may often want to debug your code. Therefore, it is advisable to use Visual Studio rather than Visual Studio Tools for Applications, because Visual Studio allows you to attach the debugger to SharePoint to debug an InfoPath browser form, while Visual Studio Tools for Applications does not.

 


Related Posts

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services