<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kit Menke&#039;s Blog &#187; Prototype</title>
	<atom:link href="http://kitmenke.com/blog/tag/prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://kitmenke.com/blog</link>
	<description>Experiences with SharePoint, web development, and programming</description>
	<lastBuildDate>Fri, 20 Aug 2010 14:19:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A Simple AJAX App using SharePoint Web Services</title>
		<link>http://kitmenke.com/blog/2010/03/14/a-simple-ajax-app-using-sharepoint-web-services/</link>
		<comments>http://kitmenke.com/blog/2010/03/14/a-simple-ajax-app-using-sharepoint-web-services/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 00:08:18 +0000</pubDate>
		<dc:creator>Kit</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://kitmenke.com/blog/?p=224</guid>
		<description><![CDATA[In this era of Web 2.0 web applications, I think it was only a matter of time before I needed to investigate how I could apply AJAX to SharePoint. With a little JavaScript and SharePoint&#8217;s web services, you can build fancy apps that can easily be dropped into a document library. And after reading Glen [...]]]></description>
			<content:encoded><![CDATA[<p>In this era of Web 2.0 web applications, I think it was only a matter of time before I needed to investigate how I could apply AJAX to SharePoint. With a little JavaScript and SharePoint&#8217;s web services, you can build fancy apps that can easily be dropped into a document library. And after reading <a href="http://blog.glenc.net/2007/04/20/calling-sharepoint-web-services-from-javascript/">Glen Cooper&#8217;s blog post</a> on the subject, I figured it was something definitely worth investigating.</p>
<p>I also took this opportunity to improve a process I have to do all the time: what are the internal column names in a list? To get the list information, I built a simple AJAX app that consumes the Lists.asmx web service. Please keep in mind that this code will have be uploaded into  SharePoint in order to work (a document library will work) and will not run from your desktop.</p>
<p><span id="more-224"></span></p>
<h2>Step 0: HTML for user input and displaying results</h2>
<p>Since I will be calling the GetList method on Lists.asmx, I will need two parameters from the user: Site URL and List Name. Site URL gives the web service context to a particular web and List Name should be a list within that web. I also specify a DIV which is where I will display the list&#8217;s information.</p>
<pre lang="html4strict" escaped="true">&lt;div id="controls"&gt;
   &lt;fieldset&gt;
      &lt;legend&gt;List Information&lt;/legend&gt;
      &lt;label for="txtSiteURL"&gt;Site URL:&lt;/label&gt;&lt;input id="txtSiteURL" type="text" style="width:400px" value="http://server/sites/SiteCollection/SubSite" /&gt;&lt;br/&gt;
      &lt;div&gt;Example: http://server/sites/SiteCollection/SubSite&lt;/div&gt;
      &lt;label for="txtListName"&gt;List Name:&lt;/label&gt;&lt;input id="txtListName" type="text" value="Images" /&gt;&lt;br/&gt;
      &lt;div&gt;Example: Images&lt;/div&gt;
      &lt;input id="btnExecute" type="button" value="Get Columns" /&gt;
   &lt;/fieldset&gt;
&lt;/div&gt;

&lt;div id="result"&gt;
   (results)
&lt;/div&gt;</pre>
<p>Now that we have our HTML form, we can work on the JavaScript code.</p>
<h2>Step 1: Building a SOAP Message</h2>
<p>To communicate with the web service, we need to use SOAP. Luckily, Glen has already created a very handy class for taking care of these operations. It is also reusable so it will come in handy for other web service calls. I tweaked his code a little bit and then put it into SoapUtils.js (also, please note this code depends on <a href="http://www.prototypejs.org/">Prototype.JS</a>):</p>
<pre lang="javascript" escaped="true">/*
SoapUtils.js
Author: Glen Cooper

http://blog.glenc.net/2007/04/20/calling-sharepoint-web-services-from-javascript/

*/
var Soap = {
   createEnvelope: function(action, ns, parameters) {
      var soap = '&lt;?xml version="1.0" encoding="utf-8"?&gt;';
      soap += '&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;';
      soap += '  &lt;soap:Body&gt;';
      soap += '    &lt;' + action + ' xmlns="' + ns + '"&gt;';
      soap += Soap.__parseParameters(parameters);
      soap += '    &lt;/' + action + '&gt;';
      soap += '  &lt;/soap:Body&gt;';
      soap += '&lt;/soap:Envelope&gt;';
      return soap;
   },

   __parseParameters: function(parameters) {
      var params = "";
      if (typeof parameters == 'object') {
         // check if we were provided an array or an object
         if (typeof parameters.push == 'function') {
            for (var i = 0, length = parameters.length; i &lt; length; i += 2) {
               params += "&lt;" + parameters[i] + "&gt;" + parameters[i+1] + "&lt;/" + parameters[i] + "&gt;";
            }
         }
         else {
            $H(parameters).each(function(pair) {
               params += "&lt;" + pair.key + "&gt;" + pair.value + "&lt;/" + pair.key + "&gt;";
            });
         }
      }
      return params;
   }
}</pre>
<p>This utility class handles a lot of the grunt work for creating the SOAP message. It makes it pretty easy to grab the input, build the message, and then send it to the server. Next, I need to give it the specifics it needs to build the actual message. This is all contained inside my Execute function:</p>
<pre lang="javascript" escaped="true">function Execute() {
   $('result').innerHTML = "Executing..."

   var txtSiteUrl = $('txtSiteURL').getValue();
   var txtListName = $('txtListName').getValue();

   // build parameter object
   var parameters =
   {
      listName: txtListName
   }

   // create soap envelope
   // can find the action and namespace from the WSDL page
   // ex: http://servername/_vti_bin/Lists.asmx?op=GetList
   var soap = Soap.createEnvelope(
     "GetList", // action
     "http://schemas.microsoft.com/sharepoint/soap/", // namespace
     parameters);

   if (!txtSiteUrl.endsWith('/')) {
      txtSiteUrl += '/';
   }
   txtSiteUrl += '_vti_bin/Lists.asmx';

   // call web service
   new Ajax.Request(
     txtSiteUrl,
     {
       method: "post",
       contentType: "text/xml",
       postBody: soap,
       onSuccess: ajaxSuccess,
       onFailure: ajaxError
     });
}</pre>
<p>The Execute method is then bound to the &#8220;Get Columns&#8221; button on page load:</p>
<pre lang="javascript" escaped="true">Event.observe(window,'load',function(){
   Event.observe('btnExecute','click',Execute);
});</pre>
<h2>Step 2: Parsing the Web Service&#8217;s XML response</h2>
<p>At this point, we are ready to handle the response from the web service. Unfortunately, parsing XML using JavaScript was not exactly the easiest experience. I haven&#8217;t found an easy way to do it, so if you have any ideas, please let me know in the comments.</p>
<p>In any case, if an error occurs, I simply want to print the error message:</p>
<pre lang="javascript" escaped="true">function ajaxError(result) {
   Element.insert($('result'), {bottom:'&lt;span style="color:red"&gt;' + result.responseText + '&lt;/span&gt;'});
}</pre>
<p>A success message is a lot more complicated since we want to extract information out of the returned XML. In fact, if you look at the entire response from the webservice after calling GetList, the returned result for a simple Images library is something like 1100 lines of XML. The majority of this is taken up by the full description of each column in the list. There is a LOT of junk that we are simply going to be skipping over. For sanity sake, I&#8217;m not going to post the entire response, but useful snippets of interest.</p>
<p>Overall, the response will look something like:</p>
<pre lang="xml" escaped="true">&lt;GetListResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"&gt;
  &lt;GetListResult&gt;
    &lt;List RequireCheckout="False" EnableMinorVersion="False" ShowUser="True" Ordered="False" MultipleDataList="False" Hidden="False" EnableVersioning="True" EnableModeration="False" EnableAttachments="False" AllowMultiResponses="False" AllowDeletion="True" HasUniqueScopes="False" WorkFlowId="" MajorWithMinorVersionsLimit="0" MajorVersionLimit="0" ScopeId="317f19e2-4ba8-4ecc-82f3-9d846a270840" SendToLocation="" WebId="0bb1435c-d56a-4f6a-93a7-77ad6990a48c" WebFullUrl="/sites/SiteCollection/SubSite1/SubSite2" EmailAlias="" EmailInsertsFolder="" EventSinkData="" EventSinkClass="" EventSinkAssembly="" Author="630" WriteSecurity="1" ReadSecurity="1" RootFolder="/sites/SiteCollection/SubSite1/SubSite2/Images1" AnonymousPermMask="0" ItemCount="6" Flags="4232" WebImageHeight="480" WebImageWidth="640" ThumbnailSize="160" Direction="none" Version="0" LastDeleted="20081001 01:58:47" Modified="20100205 08:40:53" Created="20081001 01:58:47" ServerTemplate="109" FeatureId="00bfea71-52d4-45b3-b544-b1c71b620109" BaseType="1" Name="{2CDA4792-6D4E-4D9F-BC5B-F862D5235061}" ImageUrl="/_layouts/images/itil.gif" Description="" Title="Images" ID="{2CDA4792-6D4E-4D9F-BC5B-F862D5235061}" MobileDefaultViewUrl="" DefaultViewUrl="/sites/SiteCollection/SubSite1/SubSite2/Images1/Forms/AllItems.aspx" DocTemplateUrl=""&gt;
      &lt;Fields&gt;
        ... all fields are listed here in &lt;Field&gt; nodes
      &lt;/Fields&gt;
      &lt;RegionalSettings&gt;
         ...
      &lt;/RegionalSettings&gt;
      &lt;ServerSettings&gt;
         ...
      &lt;/ServerSettings&gt;
    &lt;/List&gt;
  &lt;/GetListResult&gt;
&lt;/GetListResponse&gt;</pre>
<p>Notice, the List node contains a LOT of useful information. Once we have that node, we can display a lot of it using this function:</p>
<pre lang="javascript" escaped="true">function addListInformation(nodeList) {
   var listTitle = nodeList.attributes.getNamedItem('Title').value;
   var listImg = nodeList.attributes.getNamedItem('ImageUrl').value;
   var listDesc = nodeList.attributes.getNamedItem('Description').value;
   var listCount = nodeList.attributes.getNamedItem('ItemCount').value;
   var listViewURL = nodeList.attributes.getNamedItem('DefaultViewUrl').value;

   var output = '&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="' + listViewURL + '"&gt;&lt;img src="' + listImg + '" alt="' + listTitle + '" /&gt;&lt;/a&gt;&lt;/td&gt;'
   output += '&lt;td&gt;&lt;a href="' + listViewURL + '"&gt;' + listTitle + '&lt;/a&gt;&lt;/td&gt;';
   output += '&lt;td&gt;# of items = ' + listCount + '&lt;/td&gt;';
   output += '&lt;td&gt;' + listDesc + '&lt;/td&gt;&lt;/div&gt;&lt;/tr&gt;&lt;/table&gt;';
   Element.insert('result', {bottom:output});
}</pre>
<p>Also, since we are interested in the internal column names, we want information about each of the fields. All of the columns are listed inside the Fields node (see above). A lot of them have attributes similar to below:</p>
<pre lang="xml" escaped="true">&lt;Field FromBaseType="TRUE" StaticName="Created" SourceID="http://schemas.microsoft.com/sharepoint/v3" StorageTZ="TRUE" DisplayName="Created" Name="Created" Type="DateTime" ReadOnly="TRUE" RowOrdinal="0" ColName="tp_Created" ID="{8c06beca-0777-48f7-91c7-6da68bc07b69}"&gt;&lt;/Field&gt;
&lt;Field FromBaseType="TRUE" StaticName="Author" SourceID="http://schemas.microsoft.com/sharepoint/v3" DisplayName="Created By" Name="Author" List="UserInfo" Type="User" ReadOnly="TRUE" RowOrdinal="0" ColName="tp_Author" ID="{1df5e554-ec7e-46a6-901d-d85a3881cb18}"&gt;&lt;/Field&gt;</pre>
<p>So, for each of the Field nodes, we are interested in the StaticName and DisplayName attributes:</p>
<pre lang="javascript" escaped="true">function addFieldInformation(nodes) {
   if (nodes.length &lt;= 0) {
      Element.insert(container, {bottom:"No results to display."});
      return;
   }

   var output = '&lt;table cellpadding="0" cellspacing="0" border="1"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Display Name&lt;/th&gt;&lt;th&gt;Internal Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;';
   for (i = 0; i &lt; nodes.length; i++) {
      var attr = nodes[i].attributes;
      // there are a lot more attributes, but right now only care about the Display Name
      // and the internal name
      var displayName = attr.getNamedItem('DisplayName').value;
      var internalName = attr.getNamedItem('StaticName').value;
      output += '&lt;tr&gt;&lt;td&gt;' + displayName + '&lt;/td&gt;&lt;td&gt;' + internalName + '&lt;/td&gt;&lt;/tr&gt;';
   }
   output += '&lt;/tbody&gt;&lt;/table&gt;';

   Element.insert('result', {bottom:output});
}</pre>
<p>And finally, to bring it all together, we are calling these functions inside our success function:</p>
<pre lang="javascript" escaped="true">function ajaxSuccess(result) {
   // we're going to be adding HTML so reset this to be empty
   $('result').innerHTML = "";

   // put it in xmlDoc to make it shorter
   var xmlDoc = result.responseXML;

   // get the List node and show some of the attributes that are interesting
   addListInformation(xmlDoc.getElementsByTagName("List")[0]);
   // then select the Fields node and get all of its children (Field nodes)
   addFieldInformation(xmlDoc.getElementsByTagName("Fields")[0].childNodes);
}</pre>
<p>All together now:</p>
<pre lang="html4strict" escaped="true">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;

&lt;head&gt;

&lt;title&gt;List Information - Kit's Utilities&lt;/title&gt;

&lt;!-- Include our style reset (for fun!) and our new stylesheet --&gt;
&lt;link href="css/reset.css" rel="stylesheet" type="text/css" /&gt;
&lt;link href="css/utilities.css" rel="stylesheet" type="text/css" /&gt;

&lt;!-- Include files used for ajax, etc --&gt;
&lt;script type="text/javascript" src="scriptaculous/prototype.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="ajax/SoapUtils.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="ajax/SearchWSUtils.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript"&gt;
Event.observe(window,'load',function(){
   Event.observe('btnExecute','click',Execute);
});

function Execute() {
   $('result').innerHTML = "Executing..."

   var txtSiteUrl = $('txtSiteURL').getValue();
   var txtListName = $('txtListName').getValue();

   // build parameter object
   var parameters =
   {
      listName: txtListName
   }

   // create soap envelope
   // can find the action and namespace from the WSDL page
   // ex: http://servername/_vti_bin/Lists.asmx?op=GetList
   var soap = Soap.createEnvelope(
     "GetList", // action
     "http://schemas.microsoft.com/sharepoint/soap/", // namespace
     parameters);

   if (!txtSiteUrl.endsWith('/')) {
      txtSiteUrl += '/';
   }
   txtSiteUrl += '_vti_bin/Lists.asmx';

   // call web service
   new Ajax.Request(
     txtSiteUrl,
     {
       method: "post",
       contentType: "text/xml",
       postBody: soap,
       onSuccess: ajaxSuccess,
       onFailure: ajaxError
     });
}

function ajaxSuccess(result) {
   // we're going to be adding HTML so reset this to be empty
   $('result').innerHTML = "";

   // put it in xmlDoc to make it shorter
   var xmlDoc = result.responseXML;

   // get the List node and show some of the attributes that are interesting
   addListInformation(xmlDoc.getElementsByTagName("List")[0]);
   // then select the Fields node and get all of its children (Field nodes)
   addFieldInformation(xmlDoc.getElementsByTagName("Fields")[0].childNodes);
}

function addListInformation(nodeList) {
   var listTitle = nodeList.attributes.getNamedItem('Title').value;
   var listImg = nodeList.attributes.getNamedItem('ImageUrl').value;
   var listDesc = nodeList.attributes.getNamedItem('Description').value;
   var listCount = nodeList.attributes.getNamedItem('ItemCount').value;
   var listViewURL = nodeList.attributes.getNamedItem('DefaultViewUrl').value;

   var output = '&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="' + listViewURL + '"&gt;&lt;img src="' + listImg + '" alt="' + listTitle + '" /&gt;&lt;/a&gt;&lt;/td&gt;'
   output += '&lt;td&gt;&lt;a href="' + listViewURL + '"&gt;' + listTitle + '&lt;/a&gt;&lt;/td&gt;';
   output += '&lt;td&gt;# of items = ' + listCount + '&lt;/td&gt;';
   output += '&lt;td&gt;' + listDesc + '&lt;/td&gt;&lt;/div&gt;&lt;/tr&gt;&lt;/table&gt;';
   Element.insert('result', {bottom:output});
}

function addFieldInformation(nodes) {
   if (nodes.length &lt;= 0) {
      Element.insert(container, {bottom:"No results to display."});
      return;
   }

   var output = '&lt;table cellpadding="0" cellspacing="0" border="1"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Display Name&lt;/th&gt;&lt;th&gt;Internal Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;';
   for (i = 0; i &lt; nodes.length; i++) {
      var attr = nodes[i].attributes;
      // there are a lot more attributes, but right now only care about the Display Name
      // and the internal name
      var displayName = attr.getNamedItem('DisplayName').value;
      var internalName = attr.getNamedItem('StaticName').value;
      output += '&lt;tr&gt;&lt;td&gt;' + displayName + '&lt;/td&gt;&lt;td&gt;' + internalName + '&lt;/td&gt;&lt;/tr&gt;';
   }
   output += '&lt;/tbody&gt;&lt;/table&gt;';

   Element.insert('result', {bottom:output});
}

function ajaxError(result) {
   Element.insert($('result'), {bottom:'&lt;span style="color:red"&gt;' + result.responseText + '&lt;/span&gt;'});
}

&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;div id="container"&gt;
   &lt;div id="controls"&gt;
      &lt;fieldset&gt;
         &lt;legend&gt;List Information&lt;/legend&gt;
         &lt;label for="txtSiteURL"&gt;Site URL:&lt;/label&gt;&lt;input id="txtSiteURL" type="text" style="width:400px" value="http://server/sites/SiteCollection/SubSite" /&gt;&lt;br/&gt;
         &lt;div&gt;Example: http://server/sites/SiteCollection/SubSite&lt;/div&gt;
         &lt;label for="txtListName"&gt;List Name:&lt;/label&gt;&lt;input id="txtListName" type="text" value="Images" /&gt;&lt;br/&gt;
         &lt;div&gt;Example: Images&lt;/div&gt;
         &lt;input id="btnExecute" type="button" value="Get Columns" /&gt;
      &lt;/fieldset&gt;
   &lt;/div&gt;

   &lt;div id="result"&gt;
      (results)
   &lt;/div&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<img src="http://kitmenke.com/blog/?ak_action=api_record_view&id=224&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://kitmenke.com/blog/2010/03/14/a-simple-ajax-app-using-sharepoint-web-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autopopulate a SharePoint Form from URL</title>
		<link>http://kitmenke.com/blog/2010/03/11/autopopulate-a-sharepoint-form-from-url/</link>
		<comments>http://kitmenke.com/blog/2010/03/11/autopopulate-a-sharepoint-form-from-url/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 17:28:02 +0000</pubDate>
		<dc:creator>Kit</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://kitmenke.com/blog/?p=237</guid>
		<description><![CDATA[August 20, 2010 Update: The code below has moved to be part of a bigger library&#8230; check out SPUtility.js on CodePlex! Original Post: This post builds off of my previous post about making fields read only. In this case, I needed to autopopulate the NewForm.aspx with a value that gets passed to the form via [...]]]></description>
			<content:encoded><![CDATA[<table class="information-bar" style="margin-bottom: 5px;" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td class="information-bar-icon-cell"><img src="http://kitmenke.com/blog/wp-content/uploads/2010/08/information.png" alt="" /></td>
<td><strong>August 20, 2010 Update:</strong> The code below has moved to be part of a bigger library&#8230; check out <a href="http://sputility.codeplex.com/">SPUtility.js</a> on CodePlex!</td>
</tr>
</tbody>
</table>
<p>Original Post:<br />
This post builds off of <a href="http://kitmenke.com/blog/2009/06/02/make-a-text-field-read-only-on-editformaspx-without-spd/">my previous post </a>about making fields read only. In this case, I needed to autopopulate the NewForm.aspx with a value that gets passed to the form via URL parameters.</p>
<p><span id="more-237"></span></p>
<p>In this case, the URL that gets passed in is something like:</p>
<pre lang="text" escaped="true">/Lists/ProjectTasks/NewForm.aspx?projectID=523</pre>
<p>I wanted to take the &#8216;projectID&#8217; URL parameter and autopopulate my Project ID field with this value.</p>
<p><a href="http://kitmenke.com/blog/wp-content/uploads/2010/03/projectid.png"><img class="alignnone size-full wp-image-238" title="Project ID field" src="http://kitmenke.com/blog/wp-content/uploads/2010/03/projectid.png" alt="" width="258" height="32" /></a></p>
<p>Additionally, we will make this field Read Only so that they can&#8217;t change the value. Lastly, do this all without SharePoint designer or C# code.</p>
<p>Stick the following code in a Content Editor on your NewForm.aspx or EditForm.aspx. (Remember you can get to edit mode by appending ToolPaneView=2 on the end, like NewForm.aspx?ToolPaneView=2)</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;script type=&quot;text/javascript&quot; src=&quot;/_layouts/1033/prototype.js&quot;&gt;&lt;/script&gt;<br />
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> CustomExecuteFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
    AutopopulateInputFromURL<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Project ID'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'projectID'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> AutopopulateInputFromURL<span style="color: #009900;">&#40;</span>title<span style="color: #339933;">,</span>queryParamName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
   <span style="color: #003366; font-weight: bold;">var</span> inputs <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[title=&quot;'</span> <span style="color: #339933;">+</span> title <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">!=</span> inputs <span style="color: #339933;">&amp;&amp;</span> inputs.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
        <span style="color: #003366; font-weight: bold;">var</span> input <span style="color: #339933;">=</span> inputs<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
        <span style="color: #003366; font-weight: bold;">var</span> queryParams <span style="color: #339933;">=</span> location.<span style="color: #660066;">href</span>.<span style="color: #660066;">toQueryParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryParams <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> queryParams<span style="color: #009900;">&#91;</span>queryParamName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
           input.<span style="color: #660066;">setValue</span><span style="color: #009900;">&#40;</span>queryParams<span style="color: #009900;">&#91;</span>queryParamName<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
           <span style="color: #003366; font-weight: bold;">var</span> label <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;span&gt;&quot;</span> <span style="color: #339933;">+</span> input.<span style="color: #660066;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/span&gt;&quot;</span><span style="color: #339933;">;</span><br />
           Element.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span>input<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>before<span style="color: #339933;">:</span> label<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
           input.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
        <span style="color: #009900;">&#125;</span><br />
    <span style="color: #009900;">&#125;</span><br />
 <span style="color: #009900;">&#125;</span><br />
<br />
_spBodyOnLoadFunctionNames.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;CustomExecuteFunction&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>Depends on <a href="http://www.prototypejs.org/">Prototype.js</a>.</p>
<img src="http://kitmenke.com/blog/?ak_action=api_record_view&id=237&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://kitmenke.com/blog/2010/03/11/autopopulate-a-sharepoint-form-from-url/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Make a Text field read only on EditForm.aspx without SPD</title>
		<link>http://kitmenke.com/blog/2009/06/02/make-a-text-field-read-only-on-editformaspx-without-spd/</link>
		<comments>http://kitmenke.com/blog/2009/06/02/make-a-text-field-read-only-on-editformaspx-without-spd/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 00:09:42 +0000</pubDate>
		<dc:creator>Kit</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://kitmenke.com/blog/?p=49</guid>
		<description><![CDATA[August 20, 2010 Update: The code below has moved to be part of a bigger library&#8230; check out SPUtility.js on CodePlex! After looking at a couple of examples online, most of them require the use of SharePoint Designer to make a field read only (or to hide it). Here is a method to make a [...]]]></description>
			<content:encoded><![CDATA[<table class="information-bar" style="margin-bottom: 5px;" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td class="information-bar-icon-cell"><img src="http://kitmenke.com/blog/wp-content/uploads/2010/08/information.png" alt="" /></td>
<td><strong>August 20, 2010 Update:</strong> The code below has moved to be part of a bigger library&#8230; check out <a href="http://sputility.codeplex.com/">SPUtility.js</a> on CodePlex!</td>
</tr>
</tbody>
</table>
<p>After looking at a couple of examples online, most of them require the use of SharePoint Designer to make a field read only (or to hide it). Here is a method to make a field readonly using only <a href="http://www.prototypejs.org/">Prototype</a> (javascript library) and a Content Editor Web Part</p>
<p><span id="more-49"></span></p>
<ol>
<li>Edit the EditForm.aspx page<br />
If the Edit Page option is missing from the Site Actions menu, use the ToolPaneView=2 URL parameter.<br />
Ex: /EditForm.aspx?ToolPaneView=2</li>
<li>Add a Content Editor Web Part</li>
<li>Add the following code (in this example, &#8220;Question&#8221; is the name of my field):</li>
</ol>
<pre lang="javascript" escaped="true">&lt;script type="text/javascript" src="[YOURPATHTOPROTOTYPE]/prototype.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
function SetReadOnly()
{
    var inputs = $$('input[title="Question"]');
    if (null != inputs &amp;&amp; inputs.length == 1) {
        var input = inputs[0];
        var label = "&lt;span&gt;" + input.getValue() + "&lt;/span&gt;";
        Element.insert(input, {before: label});
        input.hide();
    }
}
_spBodyOnLoadFunctionNames.push("SetReadOnly");
&lt;/script&gt;</pre>
<p>References:</p>
<ul>
<li><a href="http://nishantrana.wordpress.com/2009/01/30/read-only-field-in-sharepoint-editformaspx/">Read only field in SharePoint EditForm.aspx</a></li>
</ul>
<hr />Update after Patrick&#8217;s comment (below). Here is an updated version of the code if you want to use it for multiple fields. You can simply call SetTextFieldReadOnly for each text field you want to hide.</p>
<pre lang="javascript" escaped="true">&lt;script type="text/javascript" src="[YOURPATHTOPROTOTYPE]/prototype.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
function SetTextFieldReadOnly(name)
{
    var inputs = $$('input[title="' + name + '"]');
    if (null != inputs &amp;&amp; inputs.length == 1) {
        var input = inputs[0];
        var label = "&lt;span&gt;" + input.getValue() + "&lt;/span&gt;";
        Element.insert(input, {before: label});
        input.hide();
    }
}
function SetReadOnly()
{
    SetTextFieldReadOnly('Title');
    SetTextFieldReadOnly('Question');
}
_spBodyOnLoadFunctionNames.push("SetReadOnly");
&lt;/script&gt;</pre>
<img src="http://kitmenke.com/blog/?ak_action=api_record_view&id=49&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://kitmenke.com/blog/2009/06/02/make-a-text-field-read-only-on-editformaspx-without-spd/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using Prototype&#8217;s Element.insert</title>
		<link>http://kitmenke.com/blog/2009/06/01/using-prototypes-elementinsert/</link>
		<comments>http://kitmenke.com/blog/2009/06/01/using-prototypes-elementinsert/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 15:17:17 +0000</pubDate>
		<dc:creator>Kit</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://kitmenke.com/blog/?p=45</guid>
		<description><![CDATA[Strangely, Prototype&#8217;s documentation for Element.insert does not have any examples, so I cooked something up quick: &#60;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd"&#62; &#60;html&#62; &#60;head&#62; &#60;title&#62;Template&#60;/title&#62; &#60;script type="text/javascript" src="prototype.js"&#62;&#60;/script&#62; &#60;script type="text/javascript"&#62; Event.observe(window, 'load', function() {     // Using Element.insert to insert after, before, top, or bottom     Element.insert($('para1'), { before: "&#60;p&#62;This is paragraph [...]]]></description>
			<content:encoded><![CDATA[<p>Strangely, Prototype&#8217;s documentation for Element.insert does not have any examples, so I cooked something up quick:</p>
<pre lang="html4strict" escaped="true">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;

&lt;head&gt;

&lt;title&gt;Template&lt;/title&gt;

&lt;script type="text/javascript" src="prototype.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;

Event.observe(window, 'load', function() {
    // Using Element.insert to insert after, before, top, or bottom
    Element.insert($('para1'), { before: "&lt;p&gt;This is paragraph zero.&lt;/p&gt;" });
    Element.insert($('para1'), { after: "&lt;p&gt;This is paragraph two.&lt;/p&gt;" });
   
    // notice we have to insert using the content container
    Element.insert($('content'), { top: "&lt;p&gt;This is the first paragraph.&lt;/p&gt;" });
    Element.insert($('content'), { bottom: "&lt;p&gt;This is the last paragraph&lt;/p&gt;" });
});

&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;div id="content"&gt;
    &lt;p id="para1"&gt;This is paragraph one.&lt;/p&gt;
    &lt;p id="para3"&gt;This is paragraph three.&lt;/p&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<img src="http://kitmenke.com/blog/?ak_action=api_record_view&id=45&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://kitmenke.com/blog/2009/06/01/using-prototypes-elementinsert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
