Sitecore – When You Are Not The Super User

Again I interact with Sitecore. This time because the project (still the same as before) is published in other server (not our server) and we have so limited access to it, I took liberty to create a small “Admin Panel”-like place for my client to get the data they need.

The basic requirement is simple. Our client just want to see the list of users that have been registered in their web. Before this page is exist, we tried to look on the after registration page through google analytics. Then it will cause us a lot of time and a lot of effort with a minimal result. Then the requirement come and as I have written earlier, they want to see the list of users.

The main thing in list of users must be email address, username, and registration date. The hardest yet still easy part to get is the registration date of every user. Basically, sitecore CMS automatically saved the registration date of every user. But, only those who are allowed that can see it in the back end. Since I am not the super admin, I made some modification on the way my users register. Using the code below, I can record the date of register.

newUser.Profile.SetCustomProperty("registerDate", DateTime.Now.ToString());

On the “Admin Panel” then use the code below.

List<User> listUser = Sitecore.Security.Accounts.UserManager.GetUsers().Where(o => o.Name.ToLowerInvariant().StartsWith("YourDomainName\\") && !o.Name.ToLowerInvariant().Contains("anonymous")).ToList();

aRepeater.DataSource = listUser;

Well, It is quite easy. But it bug me quite a short time to figure that up. The bad thing is that the custom property can only contains string. Means we can really use the date time easily. If you encounter the same requirement, please create the custom property from the start. Why? Because it is needed for the user custom property data to be recorded from the start. If you have already create a registration and encounter the same problem as me, doing the same method, then handle your exceptions well.

This custom property on user is quite useful too. One can store some things inside, as long as it is a string. Well, if you have other way to access it, then please share it 🙂

Sitecore – Shortening URL (for Your SEO Sake)

Hello, all! Tonight seems like the time for me to unleash what I have discovered during my time of blogging-hibernation. Earlier, I talked about WURFL and sitecore’s default way to detect device, now I talk about the way to shorten URL in Sitecore. This way bug me off and I left my question in stackoverflow left unanswered.

I am really aware that there is a default way for Sitecore to perform such task. What I found is my URL is really long and my boss is quite angry for it mess the SEO. Below is the default way.

UrlOptions opt = (UrlOptions)UrlOptions.DefaultOptions.Clone();
opt.SiteResolving = Sitecore.Configuration.Settings.Rendering.SiteResolving;
linkToResep.NavigateUrl = LinkManager.GetItemUrl(citem, opt);

Now, as you see, you need to configure the config too. So that it will shorten the URL as you wish. You can look on my question for detail.

I got various answers from kind people in my question, but, I still having trouble in it. So I am enlightened when my boss said that in PHP we can regex match something. Well, yeah! I will use regex to remove those long link!

As default we know that Sitecore LinkManager will produce a long link that contains /sitecore/content/sites/yoursite/ to access your items. Well, it seem so suck to the SEO. Then we should remove it. Below is how.

  1. Create a link using Sitecore.Links.LinkManager.GetItemURL(myItem). The output will be used as a parameter to my method that will shorten the url.
  2. Create a method that will receive a link and will shorten it for sure. I suggest you to create a helper class that will contain all functions and helpful methods that you need in many classes.
    public static string ShortenURL(string URLToShorten)
        return Regex.Replace(URLToShorten,@"sitecore/content/sites/[\w]{1,}/","");
  3. The call will be like below.
    yourHyperLinkObject.NavigateUrl = your.helper.ShortenURL(Sitecore.Links.LinkManager.GetItemURL(myItem));

Well, I tested it already, and thank you to the regexpal that ease my testing.

Wait, wait, wait, wait. Hold on! That easy? Yes. Why I didn’t find it out so quickly? Well, people, I didn’t think out of the box. I was when I found this out through my boss’ words about PHP. Okay, problem solved? Not yet! Look on the title, for your SEO sake. We just shorten the URL and not finished optimizing the SEO.

If you are Sitecore developer, then you must have known that you can access your site using both and Okay, you knew it already, then what about it. As soon as I can shorten the URL, my boss feel relieved, and he asked whether the old long link still accessible or not. I said, yes. He panicked!

What must I do, first, you need to add canonical to your site tag. The canonical will contains the long link. Second, if you must, you need to put a redirection whenever someone is trying to access the long link to the correct short link.

Using this, well, your SEO is getting better and not screwed up a lot. I hope this is useful to you all folks! Please feel free to comment 🙂

Sitecore – WURFL or Sitecore Device Default Detection

Hi all! I return from my long blogging-hibernation. Lately, I encounter a little problem with mobile detection in Sitecore. Before that, yes, I am still working on the website of my company’s client and yes, though I really tired of this Sitecore stuffs, I still feel so curious about it. I hope through this post, you can find the best way to choose for your Sitecore site in detecting device.

As we know, WURFL is a powerful tool that makes your web page suit the device that opens it. We also know that Sitecore is powerful enough to let the developers set a different look and feel for a different device. I thanked so much to my client for they have provided us, the developers a good-customized-WURFL-system. Our client, that is mainly based in the continent far away customized the 51Degree to suit their needs. I don’t want to write how to do such because greater people than me have written about it in their blog (example: the great mentor – at least for me – Alex’s blog.)

Then using the module that our client have made, we can differentiate the site look and feel on various devices. Well, for now, we just differ between mobile, tablet, and our beloved big screen computer. Well, it works just great. Our client make a fine module and I like it! Well, somehow there is a problem, what if the device have a different user agent and that user agent is not registered, yet? Also the update can be automatically done.

In contrast, you know that most of the devices have a common thing in their user agent. Really? What is it? For example, there will be words like “mobile”, “Blackberry”, “Android”, “iP”, etc. You can add the devices and don’t have to write a module like what my client have done. It can detect automatically, and easily. Really? Yes. So, I tried this, too! The result is just great! Fine and really nice. The goodness is that you can detect most of devices easily because they “must have something in common” on their user agent. What’s the disadvantage? If you add a new device, well, be sure to update all of the items in your Sitecore, which can be so many even though you only update the default value of the template.

In the last two paragraphs, it shows like I am telling you that Sitecore default device detection using user agent is a classic way that cost us more effort than using the WURFL way to detect device. Well, I’m afraid I have to say that is true. Since 51Degree offers a free lite package to use, we can just create a module that will need to be updated once or twice for a long time. We don’t have to really create a new device and trouble us with all the task to assign the layouts and sub-layouts to our Sitecore items.

Well, it is up to you to choose. But I am sure there are more things to consider than just what I wrote in here. Be free to comment.

May, 29 2013 update:
For some reason, the device detection system on my local is having problem. Sometimes it works out, sometimes it don’t. I tried to check the values of IsMobile and width (these two rules are those who are used in detecting mobile device in my project). It shows values that match the rule, but somehow it keeps showing the default layout setting from Sitecore. Will update on the way to prevent it.

TFS – My View (1)

It is funny when I use TFS from Microsoft. Not I want to judge them badly, I am just saying my opinion. Well I am using that because it is one of the requirement from a very influenced client. They have implement TFS and stuffs, so I follow.

Okay, what I feel is not so good is that TFS freeze my Visual Studio every time I want to perform check-in. Well, my connection may be slow, but it is the main problem. The server out there may have been waiting for long, my machine is waiting. They both keep waiting, and stupid time. I can’t continue on my work. What should I do? Edit those codes that using Framework using notepad++? What about no?!

I think it is very important for such tasks to be done in the background. So that small developer like me wont have to wait for long to continue working.

And also I add an index (1) there at the title because I want this impression to continue. It is not always bad what this TFS had.

Sitecore – Extra Fields on Membership

Hello! Now I want to discuss about membership in Sitecore. I won’t tell in detail. I will focus in how could these membership can be used simply for some features.

In my application, there’s a membership that using extranet\ role. So I use membership class. Membership in Sitecore is basically the same with .NET Membership class. Well, they inherit .NET. So if you’ve learned and know about .NET membership class, you can make log in, log out, register, forgot password, and edit profile page easily. And also, if you want such tutorial, I won’t giving it because if you google it, there are blogs that have given step-by-step guide already. Just follow it. 🙂

Sitecore allows us to create an extra field. What is that? I don’t know exactly, but you can add a field on your user object and it will be stored in database. Let’s not discuss about how Sitecore manage it. These fields can be accessed like a dictionary (array of object with string as the key). It makes the custom fields are easier to manage. You just have to define the key then use it everywhere.

You also knew that developers can create a custom membership for sitecore. But as I look deeper, it is confusing if you only have a short time to develop a complex membership (confusing if you are new to Sitecore of course). So let’s just use this custom field where you can add values just by adding fields needed. For example in my app, I want the user have phone number and address. Should I create a custom membership and let even the administrative users have phone number and address? I don’t think so. I could just add two fields every time an extranet\ user register their account. I can edit those two fields anytime I want to.

For now I can’t give you the code example because I am not at my office and I left the code there. I promise will edit this tomorrow.

To create those extra field, you can modify the code below.

Sitecore.Security.Accounts.User user = authHelper.GetActiveUser();
user.Profile.SetCustomProperty("key", "value");

Sitecore – Web Service

I don’t know whether I should hold my post for another day or I keep posting everything. But well, I am in mood to post things, so here it is.

Somehow, I develop a web that using javascript to run a serverside method. Yes, sometimes you need that. I figure it out how to use that and I got the answer: web service.

Sitecore has provide its developer with a web service to access some functions. You can find it on /sitecore/shell/Webservice/Service.asmx Well they provide some useful functions I told you and you might need that. But those functions are not always needed (I guess). For me, (I don’t know about others) I use web service to access data that my application provide not the data that CMS provide for my application. For example, sitecore is providing me the list of users, I really don’t need to use web service (at least until now) to access that. My application provide a list of products and I need to access them from anywhere. Oh, and by the way, if you use the web service that Sitecore provide you will be logged in as sitecore/* user.

So, I create a custom web service in my application. Just say that the web service provide me a function that returns a list of string. I have to call it using javascript. How do I do that? Here’s the step.

  1. Add [System.Web.Script.Services.ScriptService] tag before your web service class definition. So it will become like the code below.

    public class ClassName : System.Web.Services.WebService
  2. You need to register your service in your web control. Using the code below, you can register the web service as a script on a control.

    <asp:ScriptManager ID="_scriptManager" runat="server">
        <asp:ServiceReference Path="/path/to/your/web/service.asmx" />
  3. Then you may call it using javascript using the code below.


    Params is all parameters needed by your web service method. OnSuccess is a function that will process the result if there is no error. OnFail is a function that will process the result if there is any error. And I haven’t used userContext. 😀

  4. the function to process the result will be like

    function FunctionName(result) {
        /* body */

Simple right? Just four steps to use it. You can do most of anything. And if you want to use the web service from the server side C# code, it becomes even simpler. You only need to add reference then use it as you like.