Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a list which contains a number of "Single line of text" types, these can be written with anything the user wants, but should, by default, provide a GUID value.

It seems that one cannot properly create a proper GUID value directly in javascript, so what would be the simplest method for me to automatically populate these fields with a GUID value?

share|improve this question
add comment

3 Answers

The simples way is to create SPItemEventReceiver and implement ItemAdding, ItemUpdating methods.

share|improve this answer
add comment

If you are looking to populate a form instead of providing a default value after an event has occurred you could refer to the following:

http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript

As it says these methods generally don't provide true GUID values. What I may recommend instead if you need this is to perhaps create a web service that simply returns a newly generated GUID and combine that with some jquery ajax to get the GUID and populate the field in the form.

share|improve this answer
    
Good suggestions, although as you say the first isn't really GUID. The web service idea isn't terrible either. But I'm hoping to find a simpler method. –  Dave Alger Apr 13 '12 at 12:32
add comment

If your problem is how to obtain a guid in javascript you can render a JS variable to your page from server-side code and populate it with anything you want. You can create a delegate control (here's the step-by-step guide exactly for your task) and do the following:

public class MyDelegateControl : WebControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Page.IsCallback && !Page.IsPostBack)
            {
                string script = String.Format("var myGuid = '{0}';", Guid.NewGuid().ToString());

                Page.ClientScript.RegisterClientScriptBlock(GetType(), "NewGuidsScript", script, true);
            }
        }
    }

If you need 10 guids simply create 10 variables or an array or anything you want.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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