Monthly Archive for April, 2011

Redirect from NewForm to DispForm or EditForm

03.03.2012 Updated the solution to use spjs-utility.js for compatibility with newer versions of jQuery. See download link below.
24.04.2011 I have added a few lines to take into account the dialog’s in SP2010.


I have done a few articles on this subject earlier, but here is another method which is simpler to use. What it does is to modify the form action in NewForm to redirect you to DispForm or EditForm after the form has been submitted. You are redirected without the ID parameter in the URL. When in DispForm or EditForm, the lack of an ID parameter triggers a query to find the last item added to the list by the current user.

This code examples refers jQuery from Google’s site. All other code is kept in the CEWP.

Note:
This example uses the parameter “_spUserId” which is provided by SharePoint and represents the logged in user’s ID. This code will not work for anonymous users.

Learn how to insert CEWP on NewForm, DispForm or EditForm

Start by adding this code in a CEWP below NewForm.aspx:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

function PreSaveAction(){
	var URL = location.pathname.replace('NewForm.aspx','DispForm.aspx');
	if(GetUrlKeyValue('IsDlg')==='1'){
		URL+="?IsDlg=1";
	}
	$("#aspnetForm").attr('action',location.pathname+"?Source="+URL);
	return true;
}

</script> 

This code will send you to “DispForm” when submitting the form. To have it redirect you to EditForm, replace “DispForm” with EditForm” in line 05.

In DispForm.aspx (or EditForm.aspx), add this code in a CEWP below the form:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="/YourLocalScriptRepository/spjs-utility.js"></script>
<script type="text/javascript">
// List GUID or list display name of the current list
var thisListGuid = '{A162F3E2-32D8-4CA2-B849-14879CDC5494}';

// This code runs only if the ID parameter is omitted
if(GetUrlKeyValue('ID')===''){
	var lastID = getLastItemID();
	if(lastID!==''){
		var newURL = location.pathname+"?ID="+lastID;
		if(GetUrlKeyValue('IsDlg')==='1'){
			newURL+="&IsDlg=1";
		}
		location.href=newURL;
	}
}

/********************************************
	Do not modify anything below this line
*********************************************/
function getLastItemID(){	
	var queryBuffer = [];
		queryBuffer.push("<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='User'>"+_spUserId+"</Value></Eq></Where>");
		queryBuffer.push("<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>");
	var res = spjs_QueryItems({listName:thisListGuid,query:queryBuffer.join(''),viewFields:['ID'],rowLimit:1});
	if(res.count<0){
		alert("An error occured. Most likely the parameter "thisListGuid" is wrong.");
	}else if(res.count>0){
		return res.items[0].ID;
	}else{
		return '';
	}
}
</script>

You find spjs-utility.js here. Ensure you get the latest version

If you prefer to use a local copy of jQuery, download it from here.

In line 05 you must provide the list GUID (recommended) or the display name of the current list. Learn how to find the list GUID

Ask if anything is unclear
Alexander

Toggle appended comments in multi line fields

I got this request from Tim:

Tim Says:
Is there a way to hide the appended comments to a multipe line field with version control. Actually can it a a click hide/show, and can it display the last comment by default? click it the see all pervious comments.

Larry got a bit on the way, but the appended comments are a bit different in a plain text field and in a rich text field. This approach splits up the comments and wraps them in new div’s to handle the visibility.

The end result looks like this:
DispForm:
IMG
EditForm:
IMG

Put this code in a CEWP below the form in DispForm.aspx or EditForm.aspx

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
fields = init_fields_v2();

toggleAppendOnMultiline('MyRichText');
toggleAppendOnMultiline('MyPlainMultiLine');

function toggleAppendOnMultiline(fin){
var currField = $(fields[fin]);
var rich = (currField.find('.ms-formbody div:last').parent().find('div').length>1)?true:false;
	if(rich){	
		var allArr = currField.find('.ms-formbody div:last').parent().find('div').remove();
	}else{
		var all = currField.find('.ms-formbody div:last').remove();
		var allArr = all.html().split(/<br>/i);
	}
	var buffer = ["<table style='width:410px'><tr><td valign='top' style='font-size:0.7em;width:99%'>"];
	$.each(allArr,function(i,part){
		var disp = "block"
		if(i>0){
			disp = "none";
		}
		buffer.push("<div class='"+fin+"_dummyHide' style='display:"+disp+"'>");
		if(rich){
			buffer.push($(part).html());
		}else{
			buffer.push(part)
		}
		buffer.push("</div>")
	});
	buffer.push("</td>");
	if(allArr.length>1){
		buffer.push("<td title='Toggle visibility' onclick='toggleShowAll(this,""+fin+"_dummyHide")' valign='top' style='cursor:pointer;'>");
		buffer.push("<div style='white-space:nowrap;border:1px silver solid;padding:2px;background-color:#ffffff'>");	
		buffer.push("<img style='vertical-align:middle' src='"+L_Menu_BaseUrl+"/_layouts/images/tpmax1.gif' border='0'>");
		buffer.push("<img style='vertical-align:middle' src='"+L_Menu_BaseUrl+"/_layouts/images/tpmax1.gif' border='0'>");
		buffer.push("</div>");
		buffer.push("</td>");
	}
	buffer.push("</tr></table>");	
	currField.find('.ms-formbody').append(buffer.join(''));
}

function toggleShowAll(elm,id){
var img = $(elm).find('img');
	if(img.attr('on')!=='1'){
		img.attr('src',L_Menu_BaseUrl+"/_layouts/images/tpmin1.gif");
		img.attr('on','1');
	}else{
		img.attr('src',L_Menu_BaseUrl+"/_layouts/images/tpmax1.gif");
		img.attr('on','0');
	}
	$("div."+id+":first").nextAll().toggle();
}

function init_fields_v2(){
	var res = {};
	$("td.ms-formbody").each(function(){
	var myMatch = $(this).html().match(/FieldName="(.+)"s+FieldInternalName="(.+)"s+FieldType="(.+)"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>0){
					type=type+"_HTML";
				}
			}
			if(type=='SPFieldLookup'){
				if($(this).find('input').length>0){
					type=type+"_Input";
				}
			}
			// Build object
			res[fin] = this.parentNode;
			res[fin].FieldDispName = disp;
			res[fin].FieldType = type;
		}		
	});
	return res;
}
</script> 

Please note line 5 and 6 – i have addressed my two fields by FieldInternalName there. In the images, the comments in the field “MyRichText” has been toggled visible.

Alexander




%d bloggers like this: