When a user uploads/creates a document into my document library, i need to be able to apply a metadata tag to the document either from the URL query string or from the value selected in some web-part that is also on the page.

Here is the full explanation: I want to build a web-part that reads from a list and presents a hierachical navigation tree, the bottom level nodes will map to terms in the term-store and will be links to document libraries. Multiple bottom level nodes can point to the same library. When a user clicks a bottom level node they are taken to the relevent library for that node, when they create/upload a document the appropriate metadata term (that they clicked on in the navigation tree) will be applied to the document (they can't then change it).

Does anyone have any ideas how this might be done? I thought of adding a query string to the URL but not sure how to then apply the term to the item.

I also thought of creating a custom field that would be applied to the content type of the documents being uploaded but i don't think that the code in the field would be able to 'reach' up onto the page to grab the selected value from the navigation control or get it from the URL either.

Finally i thought of an event receiver on the doc library, but again i'm not sure how to obtain the term that needs to be applied to the document,

Any help would be appreciated,


2 Answers 2


If you want to use query string parameters to set metadata, you're going to need some script that parses the query string and sets the field values. I've done this in the past with jQuery and SPServices. Marc has created a dead simple to use query string parser in the SPServices project.

Here's an example from a project I worked on that got 2 query string variables and set field values with them.

<script type="text/javascript">

var queryStringVals = $().SPServices.SPGetQueryString();
var IDtoGet = queryStringVals["theCID"];
var insEmail = queryStringVals["InstructorEmail"];
  $("textarea[Title$='People Picker']").val(insEmail);
  $("div[title='People Picker']").text(insEmail); 
}); //close doc ready
  • Thanks @Kit Menke and both of you for your suggestions, but how would I apply this within a document library? I believe that the upload form is a physical file (upload.aspx in the layouts directory). I don't want to edit it directly as i think this will effect the whole farm. If i make a copy and change that, how would specify that specific libraries use the new upload page and not the default one? Also for when a use creates a new document from the library i guess i would need to use another approach, any ideas? THanks for your help, really appreciated :-)
    – toby
    Aug 5, 2011 at 10:21
  • I'm pretty sure the upload page redirects to EditForm.aspx..
    – Kit Menke
    Aug 5, 2011 at 14:16
  • It does, that's where you can do some client side scripting. Aug 5, 2011 at 14:52

As @PirateEric mentioned, an easy way to set metadata fields using a query string is by using JavaScript. I maintain SPUtility.js which can help with setting and getting SharePoint fields. For example:

SPUtility.GetSPField('Document Category').SetValue(urlValue).MakeReadOnly();

You probably won't be able to use an event receiver because it won't have access to a query string parameter (see How to retrieve the querystring in Sharepoint itemadded event).

See also: Autopopulate a SharePoint Form from URL (with SPUtility.js) (my blog)

  • Thanks very much Kit, really appreciate your input and will take a look at SPUtility, impressed that this can be done in a one liner. Additional comments on PriateEric's post.
    – toby
    Aug 5, 2011 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.