Category Archives: Info

The SharePoint JavaScripts blog has been moved to a new server

Hi all followers!

I have decided to move the blog from wordpress.com to a self-hosted WordPress server for better control.

To ensure your receive email notifications when I post new articles, please enter your email address in the form in the top right corner of my blog

I have added a redirect on my old URL, but if you accessed this site trough the old URL, you should update your bookmark for direct access. The new blog is found here:

You find my blog here: https://spjsblog.com/
You can also follow @SPJavaScripts on Twitter

Stay tuned,
Alexander

Status update

Hi all,
I’ll be unavailable for about a week and will not answer any comments or requests during this period.

I still have a good 200 emails / comments I have not had the time to answer, and a few of you are waiting for updates on solutions.

I had hoped to publish an updated version of Dynamic Forms for SharePoint: Production which integrates Tabs for SharePoint forms v2.0, and vLookup type rollup for SharePoint during the past week, but unfortunately this did not happen.

Stay tuned and I’ll be back after a little stretch in the sun.

Alexander

Status update

Hi all,
I have been more or less absent the past few weeks, and just wanted to let you know the status.

I have published many solutions over the past years, and I get a lot of questions related to setup problems, feature requests and requests regarding updating older solutions to support new browser versions or new jQuery releases.

I have always tried to answer all requests and questions, but must draw the white flag and say that I no longer can find time to answer them all – I simply cannot spend every night in front of my computer. I have a full time job working with SharePoint, and I feel I have overdosed on SharePoint a bit lately.

I have to prioritize a bit differently, and spending time with my wife and kids outrank blogging…

I will still be answering comments and try my best to maintain the solutions I have already published, but it will be less activity regarding new solutions for a while.

Stay tuned, and it will come some new stuff – just not so often as it used to.

Alexander

How to troubleshoot when the scripts does not work

In most cases where users have trouble getting my solutions to work, the problem is the script references. I will give a few examples here on how to troubleshoot these cases.

To copy the script from sourcecode provided in the article, hover over the script and click “view source”. Highlight, copy and paste in notepad. Save with the correct file extension like this:

  • Select “Save as” from the file menu and be sure to use the “.js” extention in the file name. Below the file name, you find a drop down where you can set the file type.
  • Select “All files”. If you don not do this the file will be saved as “fielname.js.txt”.

To check that the scripts are referred correctly, insert an alert in the CEWP like this:

<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
  // Check that jQuery is loaded:
  alert(typeof($));

  // Check that a specific function is loaded:
  alert(typeof(InsertTheFunctionNameHere));
</script>

Both these alerts should give a dialog box with the text “function”. If you get “undefined”, the script reference are wrong.

Another thing:
In most cases the CEWP must be placed below the list for or list view for the code to work.

Regards
Alexander

How to use these scripts in a customized form

03.08.2010 Updated the article with another example on how to build the fields-object in a customized form.


This article describes how to use most of the solutions found in this blog with a customized form. The scripts which this article addresses, is the ones involving the function “init_fields()_v2”.

This was originally an answer to a comment in one of the articles.


I get this question a lot:
How can i use these scripts in a customized form?

The answer is simple, but i will explain it so that you understand why it’s not working.

This script relays in the function init_fields() to identify and “objectify” the form by it’s <TR> tags (table rows). What the function does is to find all the FieldInternalNames in the code by this “information-tag”:

&lt;!-- FieldName=&quot;This is the DisplayName&quot;
FieldInternalName=&quot;ThisIsTheFieldInternalName&quot;
FieldType=&quot;SPFieldText&quot;
--&gt;

It then make an object which, when called with the FieldInternalName of a field, returns an object containing that table row (both formlabel and formtable).

When initiated like this:

fields = init_fields_v2();

You address a <TR> like this:

$(fields[FieldInternalName])

Where “FieldInternalName” is the FieldInternalName of your field. Read here how to identify the FieldInternalName.

This is the original function for use in standard SharePoint forms:

function init_fields_v2(){
	var res = {};
	$(&quot;td.ms-formbody&quot;).each(function(){
	var myMatch = $(this).html().match(/FieldName=&quot;(.+)&quot;s+FieldInternalName=&quot;(.+)&quot;s+FieldType=&quot;(.+)&quot;s+/);	
		if(myMatch!=null){
			// Display name
			var disp = myMatch[1];
			// FieldInternalName
			var fin = myMatch[2];
			// FieldType
			var type = myMatch[3];
			if(type=='SPFieldNote'){
				if($(this).find('script').length&gt;0){
					type=type+&quot;_HTML&quot;;
				}
			}
			if(type=='SPFieldLookup'){
				if($(this).find('input').length&gt;0){
					type=type+&quot;_Input&quot;;
				}
			}
			// Build object
			res[fin] = this.parentNode;
			res[fin].FieldDispName = disp;
			res[fin].FieldType = type;
		}		
	});
	return res;
}

Customized form

When you modify the list form in SharePoint Designer, you loose the FieldInternalName and therefore the function init_fields_v2() does not return anything.

To address the fields on a custom form you have to find another way of addressing these fields and to “objectify” them in the same way as the script init_fields_v2(). Basically you could insert some kind of “identifier-attribute” on the <TR> tags, or you can go with the displayName – only remember that if you change the displayName – you have to update your script…

To use the a custom made ID attribute, replace the init_fields_v2() function with this one:

function init_fields_v2(){
  var res = {};
  $(&quot;td.ms-formlabel&quot;).each(function(){
  	var thisID = $(this).attr('id');
	  res[thisID] = $(this).parents('tr:first');
  });
  return res;
}

This approach requires that you add a unique ID attribute to the <TD> holding the formlabel. Here is an example of code from a customized form:

&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;&gt;
	&lt;tr&gt;
		&lt;td width=&quot;190px&quot; valign=&quot;top&quot; class=&quot;ms-formlabel&quot; id=&quot;TitleColumnCustomID&quot;&gt;
			&lt;H3 class=&quot;ms-standardheader&quot;&gt;
				&lt;nobr&gt;Title&lt;span class=&quot;ms-formvalidation&quot;&gt; *&lt;/span&gt;
				&lt;/nobr&gt;
			&lt;/H3&gt;
		&lt;/td&gt;
		&lt;td width=&quot;400px&quot; valign=&quot;top&quot; class=&quot;ms-formbody&quot;&gt;
			&lt;SharePoint:FormField runat=&quot;server&quot; id=&quot;ff1{$Pos}&quot; ControlMode=&quot;New&quot; FieldName=&quot;Title&quot; __designer:bind=&quot;{ddwrt:DataBind('i',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}&quot;/&gt;
			&lt;SharePoint:FieldDescription runat=&quot;server&quot; id=&quot;ff1description{$Pos}&quot; FieldName=&quot;Title&quot; ControlMode=&quot;New&quot;/&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td width=&quot;190px&quot; valign=&quot;top&quot; class=&quot;ms-formlabel&quot; id=&quot;ChoiceColumnCustomID&quot;&gt;
			&lt;H3 class=&quot;ms-standardheader&quot;&gt;
				&lt;nobr&gt;MyChoiceColumn&lt;/nobr&gt;
			&lt;/H3&gt;
		&lt;/td&gt;
		&lt;td width=&quot;400px&quot; valign=&quot;top&quot; class=&quot;ms-formbody&quot;&gt;
			&lt;SharePoint:FormField runat=&quot;server&quot; id=&quot;ff2{$Pos}&quot; ControlMode=&quot;New&quot; FieldName=&quot;MyChoiceColumn&quot; __designer:bind=&quot;{ddwrt:DataBind('i',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@MyChoiceColumn')}&quot;/&gt;
			&lt;SharePoint:FieldDescription runat=&quot;server&quot; id=&quot;ff2description{$Pos}&quot; FieldName=&quot;MyChoiceColumn&quot; ControlMode=&quot;New&quot;/&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

Note the id attributes “TitleColumnCustomID” and “ChoiceColumnCustomID” in line 03 and 15.

To “get” the Title field you address it like this:

fields = init_fields_v2();
var myTitleField = $(fields['TitleColumnCustomID']);
alert(myTitleField.html());

Ask if anything is unclear.

Regards
Alexander