
ID’s are a convenient, often short, way to uniquely refer to something. Unless you’re the Tax man who seems to believe he can’t get through his day without giving me, thats just one person, 7 unique id’s – presumably one for each extremity that we would like a piece off…
I digress… ID’s – SharePoint uses an ID for each item in a list and sometimes its handy to know them “Ere Bob – have you done task 1234 yet?”
You can easily add them to the list view (Modify this View then find the ID column, click display)
But what about if you want to see this on the View and Edit forms? (You can’t see it on the New form as it doesn’t get an ID assigned until you’ve created it)
You can’t do this in the UI. You can create a custom View/Edit form using SharePoint designer but its quite complex, is a pain when we add new fields and its fraught with potential problems.
So instead we’re going to look into every ones favourite SharePoint UI hacking tools – the Content Editor Web Part (CEWP) and javascript/jQuery.
Not so fast though young Jedi – before embarking on any of these hacks you should understand the pros and cons and this excellent article is a good place to start – jQuery : The SharePoint band aid.
Right now you’re back (you did read it right?) and understand what you’re getting into.
Open up the View page for any of your list items and add ToolPaneView=2 onto the end of the URL to open up the page in edit mode.
Note – if you already have a query string (&ID=xzx…) on the end of the url then you need to use &ToolPaneView=2 and if you don’t its ?ToolPaneView=2 e.g.
http://yoursite/Lists/Tasks/DispForm.aspx?ID=1
=> http://yoursite/Lists/Tasks/DispForm.aspx?ID=1&ToolPaneView=2
http://yoursite/Lists/Tasks/DispForm.aspx
=> http://yoursite/Lists/Tasks/DispForm.aspx?ToolPaneView=2
Next add a Content Editor Web Part (CEWP) to the page and put the following code into it using the Edit Source button.
Add the javascript at the bottom of this article and after you click OK you will see that ID has been added at the top of the form. Do the same on the Edit form.
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">
$(function() {
// Get the ID from the query string
var id = getQueryString()["ID"];
// Find the form's main table
var table = $('table.ms-formtable');
// Add a row with the ID in
table.prepend("<tr><td class='ms-formlabel'><h3 class='ms-standardheader'>ID</h3></td>" +
"<td class='ms-formbody'>" + id + " </td></tr>");
})
function getQueryString() {
var assoc = new Array();
var queryString = unescape(location.search.substring(1));
var keyValues = queryString.split('&');
for (var i in keyValues) {
var key = keyValues[i].split('=');
assoc[key[0]] = key[1];
}
return assoc;
}
</script>
Notes
- This has been tested in WSS3 (SharePoint 2007) and SharePoint 2010 Foundation – I would expect it to also work in MOSS/SharePoint 2010 Server.
- The method for adding a CEWP to the page in SharePoint 2010 is slightly different.
- When looking for references I found that Christophe beat me to it by about, ohh a year and a half and his version can be found here.
He doesn’t use jQuery so there is a little more code to write. If you’re just doing this or can’t use jQuery on your site (e.g. no network access) then you may be better off with his version. If you want to do other thigns on the form you may be better with the jQuery version above.
Tags: jQuery, SharePoint Development, Tip






By using ID we can view the data because ID is unique key element. How I can use procedure in our asp.net program?
Please provide help me?
Thanks
Using the web services – http://weblogs.asp.net/paulballard/archive/2005/05/08/Using-Data-From-SharePoint-2003-Lists.aspx
Or you can use the object model if your asp.net application is on the same server – SPList.GetItemByID
he he… Ryan, wanna bet that I can make my script shorter than yours, with just plain old JavaScript?
OK, just kidding, a couple lines won’t make much difference anyway, compared to the thousands of lines of jQuery. And you could shorten yours too (see the way I handle the Querystring).
More seriously, I don’t agree with your reference to the “SharePoint band aid”. This is a biased article written from a developer’s perspective.
@Christophe – Lest not start breaking out the minifier tools
You’re right though – sometimes jQuery is overused and I could be guilty as charged, m’lud!
Re: The “Band aid” article – there is a spectrum of control in corporate IT with “complete free for all” on one end and “so locked down that we might as well lock all computers in a closet and turn of the power” on the other and you see this debate time and time again the SharePoint world.
I’ve worked in corporates that are closer to the control side and seen what happens when the ‘business’ gets so fed up of not being able to do anyting that they just circumvent the IT dept so I think I take a more pragmatic view than most.
Whilst the author clearly has a point of view (as we all do) I’ve rarely seen anyone argue the case against some of these customisations so thought it an interesting POV.
Hi!
I’ve tried your solution and I’m getting “undefined” as ID value.
I’ve changed it to the russian equivalent(MOSS is in Russian) but still is getting the same error.
Thank you in advance
Also would like to add that full url when I add CEWP code is http://orpo-stand1/insForms/Lists/List7/EditForm.aspx?toolpaneview=2 and when I fulfill the form and press “OK” a new item gets created.
I think that before I create an item it can’t have any ID, so that’s why it’s complaining.
If it is right, then I don’t know how to implement showing number of the application when a user wants to fill in the form.
My task is application submitting system, by the way.
Best Regards
@Maksat – This is only for the View/Edit forms – you can’t use it on the New form because, as you’ve said, the ID value doesn’t exist at that point.
I don’t know of any good way to get the next ID value (and how do you know that someone else may not put in a new record between the new page loading and clicking OK and thereby ‘stealing’ the new ID? Instead how about creating your own MyId column using something like a GUID or a number that you can generate uniquely in JavaScript?
Thank you for answer
Well, even if I create a column myID using GUID – it still should not generate a new number before an item is created.
Or may be I am missing something?
Then the second choice would be optimal. In order to implement I need to
1. Create a global variable(by editing content web part of home page?)
2. Introduce locks – in order to prevent “stealing” values.
3. Increment the variable by editing web content of each form.
I’m a complete newbie in javascript and sharepoint, so I would appreciate any help
@Maksat – the GUID could be generated client side BEFORE the record is inserted.
The global var/locking/incrementing solution has several advantages and is the more ‘complete’ solution but is a LOT of work to do properly so only you can answer if thats worth it for your use case.
Just one example of the kind of thing you may have to worry about – suppose you need sequential ID’s (as you often do with invoices) then what happens if someone clicks on NEW, is assigned an ID out of the queue but doesn’t actually click on Save so its never used. How do you get it back into the queue of allowed values, how long do you wait?
So far I’ve been trying to find GUID site column with no luck.
Is there any special procedure to generate or retrieve a GUID?