0

I am having an issue when passing itemid as query string to controller

url is rendered like this

http://sitecorelocal/api/sitecore/Product?sc_itemid=%7BB416980B-40A7-4C8F-8F98-375DD1CD9544%7

instead of

http://sitecorelocal/api/sitecore/Product?sc_itemid={B416980B-40A7-4C8F-8F98-375DD1CD9544}

Am retrieving Id like this

            @{

             var list = @Model.Item.ID;

            }

How to avoid binding special character?

  • It is converting the { to %7B and } to %7D. You will need to replace encoded value with the { } then search sitecore with the id – Hishaam Namooya Mar 20 '18 at 14:52
  • k other than find and replace there is no option to bind with special character – sitecore-u9 Mar 20 '18 at 14:56
  • As far as i know i don’t think so because it is the browser which is encoding the value – Hishaam Namooya Mar 20 '18 at 14:57
  • Thanks one doubts is it possible to pass Item Name in Url later in controller how to get item_id with name – sitecore-u9 Mar 20 '18 at 15:06
  • You will need to search via item name but this is not recommended. – Hishaam Namooya Mar 20 '18 at 15:08
4

Your issue is that "{" and "}" are not valid characters in a URL. As such they have automatically been URL encoded when used in your query string.

Your options are

  1. Strip the brackets before appending the query string and process as required when receiving it
  2. Use a URL decode method such as HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString["sc_itemid"]);
| improve this answer | |
3

Encoding of special characters in the URL is default behavior. To have item id without special characters, you can use Sitecore.Data.ID.ToShortID() method to have short id which will not have any special characters and can solve your issue.

Another way is that remove the bracket mannually and at the time of retrieving, do string manipulation but thats not the right way.

| improve this answer | |
2

You will need to replace the encoded value. From the Id %7BB416980B-40A7-4C8F-8F98-375DD1CD9544%7D you are passing, you will replace

%7B with {

%7D with }

Then you will have the Id as {B416980B-40A7-4C8F-8F98-375DD1CD9544}. Once you have the Id in a proper state, you can then search the corresponding Sitecore Item using the Sitecore APIs.

UPDATE

If you are using Item Name in the url. This is not recommended to search via Item Name because

  • this can cost you in terms of performance.
  • you may end up with more than 1 item if item names are not unique.

One way you can use search via Item Name is if you already know the Parent Item and also if Item Name is unique.

For example, /sitecore/content/my site/products/product A and /sitecore/content/my site/products/product B and so on. So based on this, the parent item is products. So, you can build the path including the item name and search for the item: /sitecore/content/my site/products/{item name here}

| improve this answer | |
  • var database = Sitecore.Context.Database; string strProperty = Request.QueryString["Property"]; string strPropertyDisplay= strProperty.Replace("%20", " "); var PropertyItem = database.GetItem(strPropertyDisplay); – sitecore-u9 Mar 20 '18 at 15:45
  • Am getting null when searching by Name .Please correct me if am missing something – sitecore-u9 Mar 20 '18 at 15:46
  • Make sure that the item is present on the target database. If yes, check if the context database equals to your target database. Also, make sure that the name being passed to GetItem() is the same as the one you have in Sitecore Backoffice. Click on the item in Sitecore and from the Quick Info at the top you will see what’s the item name is – Hishaam Namooya Mar 20 '18 at 15:49
  • when i pass item id it works fine .but with item name am having issue.but my business scenario is to show property name in the url – sitecore-u9 Mar 20 '18 at 15:55
  • Try to use Sitecore Fast Query. Example: Item item = Sitecore.Context.Database.SelectSingleItem("/sitecore/content//*[@@name = '"strPropertyDisplay"']"); – Hishaam Namooya Mar 20 '18 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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