Passing a Source Parameter on the Query String with Multiple Other Parameters

1 minute read

I’ve posted on similar tricks to this in the past, but this little trick is really helpful and worth calling out on its own.  Say that you’d like to pass a Source parameter on the query String, as SharePoint often does for you:

http://servername/sites/sitename/Lists/MyList/EditForm.aspx?ID=275&Source=http://servername/sites/sitename/default.aspx

Simple, and you see it all the time as you navigate around in SharePoint. In many cases, you’d also like to pass additional parameters that will “travel along” with the Source parameter.  The trick for this is to simply encode the ampersand (&) character so that it won’t be treated as the start of a new Query String parameter, but instead just a part of the Source parameter. The ASCII value for the ampersand is 26 in hex, so you encode it as %26 in the URL:

http://servername/sites/sitename/Lists/MyList/EditForm.aspx?ID=275&Source=/sites/sitename/default.aspx?ProjectID=123%26Boolean=0%26EmployeeID=345

Note that I’m passing in the relative URL for the default.aspx page.  Get into the habit of using relative URLs all the time and you’ll be in much better shape in general.

With the above URL, when you are finished with the EditForm.aspx page, whether by clicking OK or Cancel, you’ll be redirected to:

http://servername/sites/sitename/default.aspx?ProjectID=123&Boolean=0&EmployeeID=345

Advertisements

37 Comments

    • vanezasuba18:

      You haven’t given me anything to go on, really. I’d suggest that you post more details and your code to one of the SharePoint forums out there. Feel free to ping me back with the link.

      M.

      Reply
  1. Mark,

    I am trying to connect two elements on a display page and the only example I have is one that show how to do it with the parm ID (Query String). My problem is that I reload this list daily so the ID could be different each day so I wants to link the two on a field called User ID instead. Can this be done and how is that done with regards to parameter source.

    In the data view parameters what would I select under parameter source?

    Control
    Cookie
    Form
    Query String
    Server Variable

    What do I specify in the next line?
    @Title <— this is the xpath variable name for User ID in the first

    Reply
    • Matt:

      I’m not sure. If it’s a DispForm, then the ID of the current item is always on the Query String. If the two sources are related, you ought to be able to use that ID as the “anchor” to find the related content in the second DataSource.

      M.

      Reply
  2. Do you have any suggestions for places where I can read up on connecting/relating the two lists for use on the form.

    Thanks

    Reply
    • Matt:

      I’m not sure I’ve ever seen the details all spelled out, but I do it all the time. The DVWP below is from the bottom of an EditForm in one of my demos. It grabs the ID from the Query String and traverses to the related child items in a second list. Hopefully this will give you enough to go on.

      M.

      <WebPartPages:DataFormWebPart runat="server" Description="" ListDisplayName="" PartOrder="4" HelpLink="" AllowRemove="True" IsVisible="True" AllowHide="True" UseSQLDataSourcePaging="True" ExportControlledProperties="True" IsIncludedFilter="" DataSourceID="" Title="Navigation Items" ViewFlag="0" NoDefaultStyle="TRUE" AllowConnect="True" FrameState="Normal" PageSize="-1" PartImageLarge="" AsyncRefresh="False" ExportMode="All" Dir="Default" DetailLink="" ShowWithSampleData="False" ListId="00000000-0000-0000-0000-000000000000" ListName="" FrameType="TitleBarOnly" PartImageSmall="" IsIncluded="True" SuppressWebPartChrome="False" AllowEdit="True" EnableOriginalValue="False" ChromeType="TitleOnly" AutoRefresh="False" AutoRefreshInterval="60" AllowMinimize="True" ViewContentTypeId="" InitialAsyncDataFetch="False" MissingAssembly="Cannot import this Web Part." HelpMode="Modeless" ListUrl="" ID="g_d9320754_bacd_40ae_92af_afebef1877a4" AllowZoneChange="True" ManualRefresh="False" __MarkupType="vsattributemarkup" __WebPartId="{D9320754-BACD-40AE-92AF-AFEBEF1877A4}" __AllowXSLTEditing="true" WebPart="true" Height="" Width=""><ParameterBindings>
      		 <ParameterBinding Name="ListID" Location="None" DefaultValue="FA5BEB9A-8E7A-464D-AD01-17E471E6687F"/>
      		 <ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
      		 <ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
      		 <ParameterBinding Name="ID" Location="QueryString(ID)" DefaultValue=""/>
      		 <ParameterBinding Name="URL" Location="ServerVariable(URL)" DefaultValue=""/>
      	 </ParameterBindings>
      <DataFields>
      @ID,ID;@ContentType,Content Type;@Title,Title;@Modified,Modified;@Created,Created;@Author,Created By;@Editor,Modified By;@_UIVersionString,Version;@Attachments,Attachments;@File_x0020_Type,File Type;@FileLeafRef,Name (for use in forms);@FileDirRef,Path;@FSObjType,Item Type;@_HasCopyDestinations,Has Copy Destinations;@_CopySource,Copy Source;@ContentTypeId,Content Type ID;@_ModerationStatus,Approval Status;@_UIVersion,UI Version;@Created_x0020_Date,Created;@FileRef,URL Path;@ItemChildCount,Item Child Count;@FolderChildCount,Folder Child Count;@Navigation_x0020_Area,Navigation Area;@Navigation_x0020_SubArea,Navigation SubArea;@Site_x0020_Link,Site Link;@SortOrder,SortOrder;@Coming_x0020_Soon,Coming Soon;@Navigation_x0020_Link,Navigation Link;</DataFields>
      <Xsl>
      
      <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
      	<xsl:output method="html" indent="no"/>
      	<xsl:param name="ID"/>
      	<xsl:param name="URL"/>
      
      	<xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
      		<xsl:call-template name="Navigation_Items"/>
      	</xsl:template>
      
      	<xsl:template name="Navigation_Items">
      		<xsl:variable name="NavigationItems" select="/dsQueryResponse/Navigation_Items/Rows/Row"/>
      		<table class="sortable" border="0" width="100%" cellpadding="2" cellspacing="0">
      			<thead>
      				<tr valign="top">
      					<th class="ms-vh" nowrap="nowrap">Sort</th>
      					<th class="ms-vh" nowrap="nowrap">SortOrder</th>
      					<th class="ms-vh" nowrap="nowrap">View</th>
      					<th class="ms-vh" nowrap="nowrap">Edit</th>
      					<th class="ms-vh" nowrap="nowrap">Title</th>
      					<th class="ms-vh" nowrap="nowrap">Coming Soon</th>
      				</tr>
      			</thead>
      			<xsl:for-each select="$NavigationItems">
      				<xsl:sort select="@SortOrder" data-type="number" order="ascending"/>
      				<xsl:call-template name="Navigation_Items.rowview"/>
      			</xsl:for-each>
      			<tfoot>
      				<tr>
      					<td class="ms-addnew" colspan="6" style="padding-bottom: 3px">
      						<img alt="" src="/_layouts/images/rect.gif"/><xsl:text> </xsl:text><a class="ms-addnew" href="../Navigation Items/NewFormCustom.aspx?Source={$URL}?ID={$ID}">Add New Item</a>
      					</td>
      				</tr>
      			</tfoot>
      		</table>
      	</xsl:template>
      	
      	<xsl:template name="Navigation_Items.rowview">
      		<xsl:variable name="FixedNavigationSubArea">
      			<xsl:call-template name="FixAmpersands">
      				<xsl:with-param name="StringValue" select="@Navigation_x0020_SubArea"/>
      			</xsl:call-template>
      		</xsl:variable>
      		<xsl:if test="/dsQueryResponse/Navigation_SubAreas/Rows/Row[@ID = $ID]/@Title = $FixedNavigationSubArea">
      			<tr class="ui-state-default">
      				<td class="ui-draggable">
      					<span class="ui-icon ui-icon-arrowthick-2-n-s">
      						<xsl:value-of select="@ID"/>
      					</span>
      				</td>
      				<td class="ms-vb" style="width:20px;">
      					<xsl:value-of select="format-number(@SortOrder, '#,##0.00;-#,##0.00')"/>
      				</td>
      				<td style="width:20px;">
      					<a href="../Navigation Items/DispForm.aspx?ID={@ID}&amp;Source={$URL}?ID={$ID}">
      						<span class="ui-icon ui-icon-zoomin"></span>
      					</a>
      				</td>
      				<td style="width:20px;">
      					<a href="../Navigation Items/EditFormCustom.aspx?ID={@ID}&amp;Source={$URL}?ID={$ID}">
      						<span class="ui-icon ui-icon-pencil"></span>
      					</a>
      				</td>
      				<td class="ms-vb" style="width:300px;">
      					<a href="{substring-before(@Site_x0020_Link, ', ')}">
      						<xsl:value-of select="@Title"/>
      					</a>
      				</td>
      				<td class="ms-vb">
      					<xsl:choose>
      						<xsl:when test="@Coming_x0020_Soon='1' or msxsl:string-compare(string(@Coming_x0020_Soon),'Yes','','i')=0 or msxsl:string-compare(string(@Coming_x0020_Soon),'True','','i')=0">Yes</xsl:when>
      						<xsl:otherwise>No</xsl:otherwise>
      					</xsl:choose>
      				</td>
      			</tr>
      		</xsl:if>
      	</xsl:template>
      
      	<xsl:template name="FixAmpersands">
      		<xsl:param name="StringValue"/>
      		<xsl:variable name="Ampersand" select="'&amp;amp;'" />
      		<xsl:choose>
      			<xsl:when test="contains($StringValue, $Ampersand)">
      				<xsl:value-of select="concat(substring-before($StringValue, $Ampersand), '&amp;', substring-after($StringValue, $Ampersand))"/>
      			</xsl:when>
      			<xsl:otherwise>
      				<xsl:value-of select="$StringValue"/>
      			</xsl:otherwise>
      		</xsl:choose>
      	</xsl:template>
      
      </xsl:stylesheet></Xsl>
      <DataSources>
      <SharePoint:AggregateDataSource runat="server" IsSynchronous="" SeparateRoot="true" RootName="" RowsName="" id="aggregatedatasource1"><Sources><SharePoint:SPDataSource runat="server" DataSourceMode="List" SelectCommand="&lt;View&gt;&lt;/View&gt;" UseInternalName="True"><SelectParameters>
      <WebPartPages:DataFormParameter ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="Navigation Items" Name="ListName"></WebPartPages:DataFormParameter>
      </SelectParameters>
      </SharePoint:SPDataSource><SharePoint:SPDataSource runat="server" DataSourceMode="List" SelectCommand="&lt;View&gt;&lt;/View&gt;" UseInternalName="True"><SelectParameters>
      <asp:Parameter DefaultValue="Navigation SubAreas" Name="ListName"></asp:Parameter>
      </SelectParameters>
      </SharePoint:SPDataSource>
      </Sources><Aggregate><concat name="data source"><datasource name="Navigation_Items" id="0" Type="SPList" /><datasource name="Navigation_SubAreas" id="1" Type="SPList" /></concat></Aggregate>
      </SharePoint:AggregateDataSource>
      </DataSources>
      </WebPartPages:DataFormWebPart>
      
      Reply
  3. Yes that is the problem, I have done it with the ID before because it is passed in the query string but what I am saying is that in this particular app the ID field will not be stable because that file will get replaced everynight with new data (not updated). The User ID field which is actually in list one field Title is the fields that I want to tie list one with elements from list two so badically if list1(Title) = list 2(User ID)

    Reply
  4. Okay perhaps I am confused…

    Two Lists (for now, will be three)

    List 1 – lots of fields but the key is a field called User ID whos xpath = /dsQueryResponse/SFDC_User_Management/Rows/Row/@Title

    List 2 – Less fields but it also has a field called User ID whos xpath = /dsQueryResponse/SFDC_Hierarchies/Rows/Row/@User_x0020_ID

    In List 1 there can only be one record with the same User ID or (@Title)

    I have a DispForm_Custom.aspx which is the display from allitems.aspx when an item is selected. On the bottom of this form I am trying to show all the items from List 2 where List 1-User ID equals List 2-User ID

    I will also be doing the same for a new list 3 soon also

    Reply
    • Matt:

      The example I posted is basically what you outline. Navigation SubAreas has child items in Navigation Items. I grab the ID of the Navigation SubArea from the Query String, look up that item, and then match on the @Navigation_x0020_SubArea value. In your case, it’ll be the UserID. The key line is this one:

      <xsl:if test="/dsQueryResponse/Navigation_SubAreas/Rows/Row[@ID = $ID]/@Title = $FixedNavigationSubArea">
      

      You can ignore the monkeying arounds with ampersands; I had to do that to get a match when the values contains an ampersand.

      M.

      Reply
  5. Not much of a code guy, have been doing this from SPD Gui, where in the code am I looking for this? First List or the Second one?

    Reply
    • Matt:

      This is definitely going to require some manual coding on your part. It’s a little hard to tell you exactly what to do, but the DVWP example shows you the general method.

      M.

      Reply

Have a thought or opinion?