Pre-Populating SharePoint fileds with values

There are occasions when we need to pre-populate SharePoint fields with data, such as from a query string. the solution is not that difficult, using a bit of Javascript. Paul blogged the solution of at Autosponge.

(Code below copied from Autosponge and full credit to Paul)

[code:html]

<script type="text/javascript">
// This javascript sets the default value of a lookup field identified
// by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
// identified by <<QUERYSTRING VARIABLE NAME>>
// Customize this javascript by replacing <<FIELD DISPLAY NAME>> and
// <<QUERYSTRING VARIABLE NAME>> with appropriate values.
// Then just paste it into NewForm.aspx inside PlaceHolderMain
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues() {
  var qs = location.search.substring(1, location.search.length);
  var args = qs.split("&");
  var vals = new Object();
  for (var i=0; i < args.length; i++) {
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
  } 
  setLookupFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);
  setTextFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);
  setPeoplePicker("People Picker", vals["<<QUERYSTRING VARIABLE NAME>>"]);
  //do not change the fieldname for setPeoplePicker
}
function setTextFromFieldName(fieldName, value) {
 if (value == undefined) return;
   var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
theInput.value=value;
}
function setPeoplePicker(fieldName, value) {
  if (value == undefined) return;
    var assignedToInput = getTagFromIdentifierAndTitle("div", "",fieldName);
assignedToInput.innerHTML = value;
}
function setLookupFromFieldName(fieldName, value) {
  if (value == undefined) return;
  var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
// if theSelect is null, it means that the target list has more than
// 20 items, and the Lookup is being rendered with an input element
  if (theSelect == null) {
    var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
    ShowDropdown(theInput.id); //this function is provided by SharePoint
    var opt=document.getElementById(theInput.opt);
    setSelectedOption(opt, value);
    OptLoseFocus(opt); //this function is provided by SharePoint
  } else {
    setSelectedOption(theSelect, value);
  }
}
function setSelectedOption(select, value) {
  var opts = select.options;
  var l = opts.length;
  if (select == null) return;
  for (var i=0; i < l; i++) {
    if (opts[i].value == value) {
      select.selectedIndex = i;
      return true;
    }
  }
  return false;
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);
  for (var i=0; i < tags.length; i++) {
    var tempString = tags[i].id;
    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
    }
  }
  return null;
}
</script>

[/code]

 

To get the code onto the page, we simply add a Content Editor Web Part onto Newform.aspx , or whichever page we have the fileds on

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

August 5. 2010 21:26

Internorm products

Ha, actions speak louder than words.

Internorm products

August 7. 2010 04:40

HYPERCOM T7PLUS MANUAL

You should wite a blog post about a recommendation of websites like Novotronix Blog - Pre-Populating SharePoint fileds with values. I came across your site two days ago when I was googling, however I am not really into the blog thing. I don't think it's because I do not enjoy blogs, but more than likely due to the fact that I'm a little ignorant to them. But blogengine is cool.

HYPERCOM T7PLUS MANUAL

August 11. 2010 23:10

vigrx

this site is really great.. nice layout

vigrx

August 11. 2010 23:11

vigrx

this site is really great.. nice layout

vigrx

August 12. 2010 16:57

e-cig

It's a beautiful country, more people need to recognise that.

e-cig

August 13. 2010 01:35

Porter Cable Battery

You should wite a blog post about a recommendation of websites like Novotronix Blog - Pre-Populating SharePoint fileds with values. I came across your blog two days ago when I was googling, however I am not really into the blog thing. I don't think it's because I do not enjoy blogs, but more than likely due to the fact that I'm a little ignorant to them. But blogengine is cool.

Porter Cable Battery

August 13. 2010 16:24

Joline Mandelik

You...are...awesome! This web site is so good. I really hope far more folks study this and get what youre declaring, due to the fact let me tell you, its important stuff. I never wouldve believed about it this way unless Id run into your web site. Thanks for putting it up. I hope you might have great accomplishment.

Joline Mandelik

August 13. 2010 19:08

Lane Terrone

this is very nice one and gives indepth information. thanks for this nice article

Lane Terrone

August 14. 2010 08:44

Hoyt Edmons

Youre not the average blog writer, guy. You definitely have anything powerful to add for the world-wide-web. Your style is so strong that you could almost get away with getting a bad writer, but youre even amazing at expressing what you might have to express. Such a great website. Ill be back for far more.

Hoyt Edmons

August 17. 2010 06:49

Wordpress Themes

So much spirit, I feel as though you've definitely hit the nail on the head. Good job with this. Please keep brining us more because we need more of your type of blogger.

Wordpress Themes

August 17. 2010 06:59

Get Ringtones

Pretty insightful post. Never thought that it was this simple after all.

Get Ringtones

August 17. 2010 07:08

Music Ringtones

Pretty good post. I just came across your site and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

Music Ringtones

August 17. 2010 08:08

Domain Name Registration

The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!

Domain Name Registration

August 17. 2010 17:39

Mario Skog

Im no expert, but I feel you just created the top point. You certainly know a whole lot about what youre discussing, and I can truly get behind that. Thanks for being so upfront and so honest concerning the subject matter matter. I truly feel like We've a better understanding now.

Mario Skog

August 18. 2010 16:01

Freeman Vallelonga

You sure do know what youre referring to. Man, this web site is just excellent! I cant wait to go through a lot more of what youve obtained to express. Im genuinely happy that I came across this when I did since I had been really starting to get bored using the whole blogging scene. Youve turned me close to, guy!

Freeman Vallelonga

August 22. 2010 06:09

Kobe shoes

What a great info, thank you for sharing. this will help me so much in my learning.

Kobe shoes

August 24. 2010 02:41

Darcy Court

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Darcy Court

August 26. 2010 23:19

Carol Burdick

Great post, bookmarked, will be back later. Because this was a fantastic article, keep up the good work. (There we go, hows that? Two sentences now, can’t call that SPAM.)

Carol Burdick

August 27. 2010 16:03

Electric Cigarette

Nice information, many thanks to the author.

Electric Cigarette

August 27. 2010 16:34

Dick Dodgson

do you have an rss feed? I want to add it to my reader but I can't find it...

Dick Dodgson

August 28. 2010 00:17

Greg Mt

why not posting more?

Greg Mt

August 28. 2010 12:31

nude cams

I was unaware of it till i read your post.Thanks for increasing my knowledge.

nude cams

August 28. 2010 12:36

nude chat

You're a very smart person!

nude chat

August 29. 2010 00:34

Napoleon Whitmyre

are you using wp for this blog?

Napoleon Whitmyre

August 29. 2010 05:29

seduction affiliate

hey, with so many comments you should add an affiliate program to your blog and make some money...

seduction affiliate

August 29. 2010 17:39

Katharine Bacco

I bookmarked your blog but I hope you will post more...

Katharine Bacco

August 30. 2010 00:09

seduction affiliate

you may make some money if you add an affiliate program to your blog...

seduction affiliate

August 30. 2010 00:27

cool blonde jokes

how can I advertise on your blog?

cool blonde jokes

August 30. 2010 12:48

jokes

nice blog! keep the good work!

jokes

August 31. 2010 00:01

Duncan Thomsen

are you using a custom template?

Duncan Thomsen

August 31. 2010 02:45

Natalya Budesa

i can't wait for taylor lautner's new movie abduction to come out...he's so awesome!

Natalya Budesa

September 1. 2010 02:06

personal training

don't spend all the day in front of your computer, go out, exercise, go to the gym twice a week...

personal training

September 1. 2010 08:43

personal training

don't forgot to get out and exercise...

personal training

September 1. 2010 20:51

Cam Girls

I have been checking out your posts for the last couple of hours, and everything has been very informative and well written. I just wanted to tell you that for some reason this one doesn't seem to work in Internet Explorer. On a side note, I was wondering if you wanted to swap blogroll links? I hope to hear from you soon!

Cam Girls

September 1. 2010 21:34

gay love

soo

gay love

September 1. 2010 23:17

Slots 777

I came across your blog two days ago when I was googling, however I am not really into the blog thing.This web site is so good. I really hope far more folks study this and get what youre declaring, due to the fact let me tell you, its important stuff.

Slots 777

September 2. 2010 03:32

test psicométrico

Hiyah, just wanted to simply drop a comment to state my opinion.I currently volunteer at a organization that made us fill out these types of test weekly, and must say that they don't provide the best results. I could not care less about what the person above me said because I know the truth. Anyways Now great site.

test psicométrico

September 2. 2010 15:37

GalPhoto

Love taking photos. Love everything about it. I love to talk about it. I love to dream about it. I see the beautful inspirational photos online and wish I can be the one taking it. yeah, that's my ultimate fantasy. SBG

GalPhoto

September 2. 2010 21:54

Jason Shakin

i'm adding your blog rss feed so that i can see your new posts. keep up the good work!

Jason Shakin

September 2. 2010 23:21

Hipolito M. Wiseman

Thank you very much for this post, was really interesting to read! :)

Hipolito M. Wiseman

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



 

Dilbert of the day