SPservices, JQuery and js Test

Nice little code to test for SPservices, JQuery and js:


<script type=”text/javascript”>
   alert ("js on.");
   $(document).ready(function() {
      alert ("jQuery on.");
      var thisSite = $().SPServices.SPGetCurrentSite();
      alert ("SPServices is on. Your site is : " + thisSite );
   });
</script>

Posted in MOSS 2007

Using SP ID in Calculated Field No Code

To use the SharePoint ID (SPID) in a calculated field, you must use a SharePoint Designer (declarative) workflow.

The Problem:

Real World example: My business partner wants the TITLE field of his items to be a human-readable ID and use it as a sort-of “natural Key.” The pattern of the ID is YYYYMMDD-00000 and is based on the Created Date where YYYY is the year, MM is a two digit month number with a leading zero and day is also two digit month number with a leading zero, and the 00000 represents a unique ID. I based the unique ID (00000) on the SharePoint item ID and that will do nicely.

Let’s get started with a calculated column based on the Created field:

Title: “CreatedYYYYMMDD”
Type: Calculated Column
Formula: =TEXT(Created,"YYYYMMDD")

Now, let’s create another calculated column to do the same thing with the SharePoint ID:

Title: “SPID00000″
Type: Calculated Column
Formula: =TEXT(ID,"00000") <-- OH NO! We cannot use ID in a calculated column!

Simple enough – let’s just create a numeric field and then use a Sharepoint Designer workflow to populate that with the Item’s SharePoint ID (SPID):

Title: “SPIDNumeric”
Type: Numeric

Now, create a calculated field based on that new field:
Title: “SPID00000″
Type: Calculated Column
Formula: =TEXT(SPIDNumeric,"00000")

Now, Open SharePoint Designer and create a workflow to run on that list whenever an item is created.

Workflow Step 1:

  • {Set Field in Current Item} Set SPIDNumeric to ID.
  • {Build Dynamic String} Store [CreatedYYYYMMDD]-[SPID00000] in Variable:NewTitle.
  • {Set Field in Current Item} Set Title to NewTitle.

Save the workflow, and then test it by creating a few items in the list.

OH NO! We cannot use SPIDNumeric this way because SPIDNumeric wont be set until after the workflow runs! – our formula returns the date as expected, but zero as the ID! 20121211-00000 for example.

How can we use the SPID in a calculated column and create our required Title? We have a few obstacles to overcome:

1. Cannot use the SharePoint Item ID in a calculated column.
2. Need a numeric field to hold the SharePoint ID so that we can use it in a calculated field.
2. Cannot use the SharePoint Item ID in a declarative workflow on a newly created item because the our newly-created ID field will not have been set until after the workflow runs once first.

The Solution:

The solution is to run the workflow twice on each item. Once to set the numeric SharePoint ID field, and once to use the numeric ID calculated column. let’s see the whole thing in action:

Create two custom lists:

1. ID generator – This is a list that will be used to generate the title for our main list:

  • Title
  • SPID: Numeric
  • SPID00000: Calculated, Single line of text, =TEXT(SPID,"00000")
  • DateCalc: Calculated, Single line of text, =TEXT(Created,"YYMMDD")

2. Main List – This is our list that we want to set the title to YYYYMMDD-00000. (we only need a title field for this example):

  • Title

Create the workflow for the ID Generator list:
Workflow Step 1:

  • {Set Field in Current Item} Set SPIDNumeric to ID.
  • {Build Dynamic String} Store [CreatedYYYYMMDD]-[SPID00000] in Variable:NewTitle.
  • {Set Field in Current Item} Set Title to NewTitle.

Create the workflow for the main list:
Workflow Step 1:

  • {Create List Item} Create item in [ID Generator List], Set Title to "Title" (Output to Variable: create)
  • {Pause for Duration} Pause for 0 days, 0 hours, 0 minutes.
  • {Update List Item} Set Title to "Title". {in the Find the list item section: Field: [ID Generator List]:ID Value: Variable: create}
  • {Set Field in Current Item} Set Title to [fx]: Source: [ID Generator List], Field: [Title], {in the Find the list item section: Field: [ID Generator List]:ID Value: Variable: create}.

Viola! We have it! Create a few items in the main list and watch as they all update themselves to the ID number we wanted…

Posted in MOSS 2007, SharePoint, WSS 3.0

Hiding FormFields in SharePoint 2007

I’ve been hiding form fields on new and edit forms.

Sometimes, I’ll use the content type method, which is a royal pain to try to configure each field as a hidden field – and you cannot hide form NEWFORM without hiding from EDITFORM and vice-versa, and the options are generally limited. SharePoint 2007 site content types are a nightmare to administer (am I changing the content type for a list? A site? The whole site collection? ¯\(°_o)/¯ I dunno lol.)

Sometimes, I’ll use the SPServices/Jquery method, which is awesome because I can have fields on my list views and data views, but not on my edit/add forms. There is lots of flexibility here.

Well, it turns out that I work for a Very Large Corporation and I have lots and lots of forms to create so that users can replace their pens and faxes with SharePoint custom lists and InfoPath forms. Each of these hide fields methods, while simple, still require too much maintenance. Each time I add a new field to a form, I have to go into the edit form and add form to update the visibility of that field if I want it hidden.

My savior: “h_” – It came to me in a dream. What if I could just add “h_” to the formfield name to have the master page pick it up and hide it from the user! (That was a rhetorical question, which is why I used a bang at the end.)

So I don’t even need SPservices for this one.

Here is the process:

  1. I just add Jquery to my masterpage (and who hasn’t at this stage in the game, I mean, c’mon, SP2k7 is O-L-D)
  2. I added a little code to check the filename (we want to make sure the user is on the NEWFORM or EDITFORM first)
  3. I added some selectors to loop through all the “nobr” tags and check the text inside for “h_” (SharePoint puts all form fields in NOBR tags, but you knew that already)
  4. Tweak Alert: Tweak the code to have the formfield display on the ADD form, but not the EDIT form (use “c_”), or have the formfield display on the EDIT form, but not the ADD form (use “u_”). This tweak is based on the general permissions model of Create(aka add)/Review(aka view)/Update(aka edit)/Delete/List (CRUDL).

Ok, so there it is.

And here is the fully working code that is running on my masterpage as we speak:

<script language="javascript" type="text/javascript">
	//<![CDATA[
	
	var path = window.location.pathname;
	var filename = path.match(/.*\/([^/]+)\.([^?]+)/i)[1];
	//Check to see if the user is on a newform or and editform:
	if (filename=='NewForm' || filename == 'EditForm'){
		//Loop through the form fields and check them:
		$('nobr').each(function(index) {
			var str = $(this).text();
			
			//Hide all SharePoint form fields that have "h_" in their title:
			if (str.toLowerCase().indexOf("h_") >= 0){
			    //alert('Hide field number' + index + ': Named: ' + $(this).text());
			    $(this).closest('tr').hide();
		    }
		    
		    //Hide all SharePoint form fields that have "c_" {only hides elements from the NEWFORM}:
		    if (filename=='NewForm'){
		    	// 
				if (str.toLowerCase().indexOf("c_") >= 0){
				    $(this).closest('tr').hide();
			    }
			    
		    //Hide all SharePoint form fields that have "u_" {only hides elements from the EDITFORM}:
		    }else if (filename=='EditForm'){
				if (str.toLowerCase().indexOf("u_") >= 0){
				    $(this).closest('tr').hide();
			    }
		    }
		});
	}
	
	//]]>
</script>
Posted in MOSS 2007, SharePoint

Hide the Cancel Button on SharePoint 2007 Forms

Wanted to hide the cancel button on all forms in our forms center site collection.

Added this to the masterpage today [also needs jquery]:

<script language="javascript" type="text/javascript">
//<![CDATA[
//Hide the cancel button on all pages:
$('input[value=Cancel]').hide();
//]]>
</script>

You can also just add it to the Editform.aspx or Newform.aspx files if you just want to hide it on one form but not the other.

Posted in MOSS 2007, SharePoint