How to Remove {generate-id()} from a Customized Form in SharePoint

If you’ve done much work using custom forms in SharePoint, you’ve probably run into the {generate-id()} bugaboo. For reasons that I’ve not been able to determine, if you use an id attribute in any HTML element, SharePoint Designer adds the {generate-id()} string to the end of the name you’ve set. Here’s an example. If you code this:

<input id="SubmitButton" type="button" onclick="javascript:DoSomething()" Value="Submit" />

when you save the page, Designer converts your code to:

<input id="SubmitButton{generate-id()}" type="button" onclick="javascript:DoSomething()" Value="Submit" />

Not all that helpful. I found a nice fix for this from Jorge (El Che) Vasquez. Simply create an empty dummy variable, like:

<xsl:param name="DummyParameter" />

and append it to your id attribute:

<input id="SubmitButton{$DummyParameter}" type="button" onclick="javascript:DoSomething()" Value="Submit" />

Designer will then give you what you wanted in the first place.

Check out Jorge’s blog post for a richer explanation of this if you need more details.

Similar Posts

17 Comments

  1. hi guys:

    first i would like to thank marc for his suggestion. but i took marc suggestion and update it now you dont have to make dummy variable, all you have to do is to add the magic expression:

    {”} put this after your ID inside the qoutations for example:

    now the generate id will not appear and also your code have nothing new because we add empty value so deal with this id as its not shown

    thank you

    1. I haven’t seen this generate-id issue show up in a very long time, and I’ve done a lot of custom form stuff in the interim. My guess is that one of the Service Packs (probably SP1) or hotfixes has made it go away.

      M.

    1. This is a really old blog post. There are good resons why {generate-id()} ought to be there. It all depends on what you are trying to do.

      M.

  2. SharePoint may think there are valid reasons for this code but it breaks our form; it breaks the our javascript validation, but it is looking for an id that does not exist. Seems to me the only possible use for the generate-id is to prevent developers from referring to elements by id.

    1. Edward:

      It’s far less insidious than that. Duplicate ids can cause far more problems than having obscure unique ones. As long as you make your ids unique yourself, SharePoint Designer won’t meddle with them.

      M.

  3. Actually SharePoint did do just that on a hard coded input with a unique ID. And when the Javascript tried to find the contents o the element it returned “‘undefined’ is null” error and stopped processing the page.

    1. I’d have to see the specifics of what you’re doing to know excatly what’s going on, but are you sure that your id is clearly unique?

      M.

  4. We wrote it as

    and the Javascript is referring to this in the line

    var user = $(“input#UserName”).val();

    SharePoint changed it to

    and when I am longed it the line read correctly but when anyone else is logged in we get

    “‘undefined’ is null”

    following guidelines in article added

    to the xsl template in which this control existed
    and changed input to

    and got

    “Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator.”

    I am not a happy camper right now.

  5. OK this web site did not read my pasted elements as text. What the above should have read is
    this was the initial element
    input type=”hidden” id=”UserName” value=”{$UserIdVar}”

    And Sharepoint changed it to

    input type=”hidden” id=”UserName{generate-id()}” value=”{$UserIdVar}”
    and
    then we made it
    input type=”hidden” id=”UserName{generate-id($DummyVar)}” value=”{$UserIdVar}”
    after
    adding parm
    xsl:param name=”DummyVar”

    Let’s see if this posts correctly.

  6. OK fixed that last to read

    input type=”hidden” id=”UserName{$Dummy}” value=”{$UserIdVar}”

    However I am still getting web part error. Any ideas?

  7. My error. In copying and pasting the param from the page above resulted in those slanted quotes instead of standard quotes which could not be evaluated. After fixing that everything worked as designed.

    1. Edward:

      Glad you worked through it! I just reformatted the post so that hopefully 1) No one else will be bitten by the weird quotes, and 2) The code is more nicely formatted.

      M.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.