Answered by:
Populate list item field with querystring

Question
-
Hi,
I got a list item with a field which I need to populate with a querystring parameter from current page.
I tried to use event reciever ItemAdded() but apparentaly there is no way to get the HttpContext.Current.Request.QueryString.
Any suggestions? I was thinking about modifying the newform.aspx, am I on the right track?
Thanks,
Peter
Friday, September 16, 2011 12:34 AM
Answers
-
A common way to address this is to retrieve the Query String in ItemAdding event and pass it to the ItemAdded event using the HttpRuntime Cache.
E.g. the following code:
public override void ItemAdding(SPItemEventProperties eventProperties)
{
// retrieve QueryString parameter
string queryString = HttpContext.Current.Request.QueryString[...];
// Get ListItem the event fired on
SPListItem currentItem = eventProperties.ListItem;
// copy query string parameter value to the cache using the unique id of the item as cache key
HttpRuntime.Cache[currentItem.UniqueId] = queryString;
}public override void ItemAdded(SPItemEventProperties eventProperties)
{
// Get ListItem the event fired on
SPListItem currentItem = eventProperties.ListItem;
// copy query string parameter value to the cache using the unique id of the item as cache key
string queryString = HttpRuntime.Cache[currentItem.UniqueId] as string;
// remove item from cache to free up space
HttpRuntime.Cache.Remove(currentItem.UniqueId);
...
}
Microsoft CSS - This post is provided "AS IS" with no warrenties and confers no rights.- Proposed as answer by Stefan GoßnerMicrosoft employee Friday, September 16, 2011 7:35 AM
- Marked as answer by Margriet Bruggeman Monday, April 9, 2012 12:54 PM
Friday, September 16, 2011 7:34 AM
All replies
-
Sure, here's a blog post I wrote about how to do that:
Pass Default Value From a Web Part Page to a New Item
Laura Rogers
SharePoint911: SharePoint Consulting
Blog: http://www.sharepoint911.com/blogs/laura
Twitter: WonderLaura
Books:Beginning SharePoint 2010: Building Business Solutions with SharePoint
Using InfoPath 2010 with Microsoft SharePoint 2010 Step by StepFriday, September 16, 2011 2:09 AM -
Thanks Laura,
I've tried this however getting some errors.
Created the parameter - all good.
removed the sharepoint field and put in
<asp:TextBox runat="server" id="ff12{$Pos}" text="{$CostBillID}" __designer:bind="{ddwrt:DataBind('i',concat('ff12',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@MatterRef')}" />
1. It doesnt actuall insert any value "CostBillID"
2. When I try to save the item, it crashes.
Could I just modify the following instead?
<SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="New" FieldName="MatterRef" __designer:bind="{ddwrt:DataBind('i',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@MatterRef')}"/>
I'm very new to sharepoint but I just can't believe how complicated something so simple is. Surely there must be some simple way to modify newform.aspx and change data around etc.
All I'm trying to do is to insert parameter into a list item which references a library. This item is like a header for each library.
- Edited by otmarp1 Friday, September 16, 2011 5:03 AM
Friday, September 16, 2011 4:58 AM -
A common way to address this is to retrieve the Query String in ItemAdding event and pass it to the ItemAdded event using the HttpRuntime Cache.
E.g. the following code:
public override void ItemAdding(SPItemEventProperties eventProperties)
{
// retrieve QueryString parameter
string queryString = HttpContext.Current.Request.QueryString[...];
// Get ListItem the event fired on
SPListItem currentItem = eventProperties.ListItem;
// copy query string parameter value to the cache using the unique id of the item as cache key
HttpRuntime.Cache[currentItem.UniqueId] = queryString;
}public override void ItemAdded(SPItemEventProperties eventProperties)
{
// Get ListItem the event fired on
SPListItem currentItem = eventProperties.ListItem;
// copy query string parameter value to the cache using the unique id of the item as cache key
string queryString = HttpRuntime.Cache[currentItem.UniqueId] as string;
// remove item from cache to free up space
HttpRuntime.Cache.Remove(currentItem.UniqueId);
...
}
Microsoft CSS - This post is provided "AS IS" with no warrenties and confers no rights.- Proposed as answer by Stefan GoßnerMicrosoft employee Friday, September 16, 2011 7:35 AM
- Marked as answer by Margriet Bruggeman Monday, April 9, 2012 12:54 PM
Friday, September 16, 2011 7:34 AM -
Your problem may be related to your field type. Is the field a lookup, by any chance? Try typing just the number in the text box and submitting the form. It will need to be number that exists as an ID in the other list you're looking up to. Since sharepoint doesn't store the actual text of the display of the lookup, and only the ID, that's why it will crash if you enter text into it. The solution unfortunately, is to pass the ID of the item as the query string parameter, and not the actual text of the item. I hope that makes sense.
Laura Rogers
SharePoint911: SharePoint Consulting
Blog: http://www.sharepoint911.com/blogs/laura
Twitter: WonderLaura
Books:Beginning SharePoint 2010: Building Business Solutions with SharePoint
Using InfoPath 2010 with Microsoft SharePoint 2010 Step by StepFriday, September 16, 2011 4:14 PM -
Thanks Stefan,
tried all that but it is not possible as the current request throws null exception.Friday, September 16, 2011 9:20 PM -
Thanks Laura,
my field is a standard text so I should be able to pass in anything.
Friday, September 16, 2011 9:26 PM