How to generate custom unique ID

一世执手 提交于 2019-12-02 04:44:22

问题


We are using Sitecore 7.2 with multi-site implementation. The actual data is shared between multisite, hence it's been stored in common Global Item folder.

We are facing a problem generating aunique ID on URL. I had a good search but could not find any solution except to use Sitecore Item ID.

This is what we want:

domain/players/player_id e.g. domain/players/1234

where 1234 is uniquely generated ID.

Could someone please suggest if this is possible?


回答1:


Every page that is managed in Sitecore is a Sitecore Item. As such, you should be able to just navigate to the name of the player item. If you were trying to say in your post that player items are stored in globals and not as pages, then you are left with the following options:

  1. Query String: domain/players/player?playerId={ID}

    • If this is the route that you choose to take then I would suggest using the player item's Sitecore ID for the value of the query string parameter.
    • If you have other IDs then put the specified ID there, however it would be easiest with Sitecore IDs
    • What you would then do is get the item with the ID specified in the query string parameter (or get the item with the player ID specified in the query string parameter, depending on which route you take) and display the data for that player on the page
      • Sitecore.Context.Database.GetItem(Request.QueryString["playerId"])
      • playerItems.FirstOrDefault(playerItem => playerItem["Player ID"] == Request.QueryString["playerId"])
        • Note that this assumes that the Player ID is a field, not the Sitecore ID
        • If it is the Sitecore ID then change the lambda to use playerItem.ID == new ID(Request.QueryString["playerId"]
      • Regardless of which one you use, I suggest adding null checks to the QueryString getter

  2. Sublayout Parameters
    • If you use this method, the query string will not change and people will not be able to get to the page from the direct URL
    • The process is the same as for query strings, except that you are using Sublayout parameters instead
    • Note that you must set these in a parent sublayout or in Sitecore (which means that you have a separate page for each player - i.e. this would be a bad solution)

  3. Virtual Items
    • This is really what I think you are looking for
    • This can be a lot of work if you do it from scratch, or you can use the Wildcard Module
    • If you do it from scratch, you will need a custom pipeline and/or processor for handling the requests



回答2:


Good suggestions from Zachary. I will add a couple more:

1) IIS Rewrite Module. If what you are really after is having external URLs look like /domain/players/1234, you could easily accomplish this by forwarding these requests to something like Zachary's option #1. The client sees /domain/players/1234, but it's really handled by a single Sitecore item at /domain/player/player.aspx?playerid=1234. Client doesn't have to know that.

2) Custom ItemResolver pipeline handler. Custom Pipelines may be a bit intimidating at first, but they are actually pretty easy to implement and highly useful. Would be pretty straightforward to add a new one which checked for "players/1234" and set the ContextItem to your player handling page and drop the ID into a session variable or some context variable.



来源:https://stackoverflow.com/questions/26140189/how-to-generate-custom-unique-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!