Here’s the scenario:
I have a form which contains a textbox. This textbox should be populated with a query string value. Users should not be able to edit the value of the textbox while adding the item.
Solution:
The above can be achieved using a Content Editor Web Part (CEWP) and jQuery.
Steps:
1. Append the NewForm.aspx url with the following: &PageView=Shared&ToolPaneView=2
2. This will cause the ‘Add Web Part’ tool pane to show up.
3. Add a CEWP to the page.
4. Go the webparts properties and click the Source button
5. Add the following:
// Note: replace this url with the url of the jquery library residing on the site!!
<script type="text/javascript" src="http://intranet/InternalDocuments/jquery-1.2.6.min.js"></script>
<script>
// This method retrieves the querystring value. If the querystring is missing it returns null. I’m not the author of the method; found it on the net, can’t remember from where though :)
function getQuerystring(key, default_)
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return 'null'
else
return qs[1];
}
//Note: The title attribute of my textbox is called ‘Source’. Replace it with the title attribute of the textbox on your form. The query string name is all called ‘Source’, replace as appropriate.
$(document).ready(function() {
$("input[title$=Source]").val(unescape(getQuerystring('Source','')));
$("input[title$=Source]").attr('readonly','true');
});
</script>
6. Hit the Save button
7. Expand ‘Appearance’ and change ‘Chrome Type’ none. This step is optional.
Technorati Tags:
jQuery,
CEWP,
Query String,
SharePoint