Wednesday 29 May 2013

Retrieve full img url from sitecore for display on site

sitecore can be quite counter intuitive at times and this is one big example of it. 

If you want the actual img url to display on your web site using an <img src="" /> you won't find that in the metadata carried with the MediaItem object. 

Instead you have to make a separate call to sitecore like this:

Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem)

and remember you can always get a MediaItem from an ImageField if that is what you have by doing imageField.MediaItem. 

Sunday 26 May 2013

Creating a git repository in an existing directory with files

Git is pretty smart and will let you very easily hook up existing source code to a new repository without making you go through some awkward copy and paste process.

all you have to do is navigate to your directory with the source files and type:

git init

then you need to tell git which repository to hook it up to:

git remote add origin [repository url]

lastly you want to add an upstream so it knows which branch to hook the code to. you can always commit on the fly to any given branch, however, you may want to set your upstream for convenience and you can do it all in one go when you do your first push like this (this example sets the master branch as the default upstream):

git push --set-upstream origin master

all done! gotta love git.

Saturday 25 May 2013

Calculating age in C#

This is a silly one, but it's amazing how often it comes up.

so if you want to know how what's the best algorithm for calculation someone's age based on date of birth, her goes. it's in C# but it can be easily adapted.

DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;
return age;

Friday 24 May 2013

HttpModules not firing under Cassini

If you are using Visual Studio 2010 this could be because 2010 Cassini uses IIS 6.0 syntax to register the HTTP modules. So if you have IIS 7 syntax in your config it'll just happily ignore it without flagging it.

check your config and update your syntax to IIS 6.0 and it should all work again.

Tuesday 21 May 2013

Git : Quick Reference


If you're new to git follow this to get up and running in a min:

set up a new repository: git clone repository_url

(code... code... code...)
 
check pending changes: git status -s

add files to the changeset: git add .
Notes:
1) not restricted to new files only; all files including existing modified ones need to be explicitly added before being commited
2) The dot in the end is part of it, not a typo. It just makes it recursively add all changed files from this dir and its subdirs

committing to the local repo: git commit -m "a message"
 
pull latest code from remote repository: git pull

(merge... merge... merge...)

push to the remote repository : git push

voila!

a few other ones that may be helpful: 

check what branch you're on: git branch

switch to a different branch: git checkout branch_name

happy git!

Bypassing publishing when updating items programatically in Sitecore

if you want to just update items in the Sitecore database without having to publish the changes all you need to do is deal directly with the master database by switching Sitecore.Context to point to it by using the Sitecore built-in factory like this:

 Sitecore.Context.Database = Factory.GetDatabase("master");

now whenever you access Sitecore.Context it'll act upon the master database. you may need to do a security override depending on what you are doing. check my previous post for instructions on how to do that.

Editing items programatically from sitecore : security exception


When using Sitecore.Context to update items you may run into a security exception saying that the ASP.Net process doesn't have permission to update the database.

You can get around that very easily by using a security override by wrapping your code in this block:

 using (new Sitecore.SecurityModel.SecurityDisabler())
{
  //retrieve item

  item.Editing.BeginEdit();

  //update item

 item.Editing.EndEdit();
}

This is likely to happen if you are using the Sitecore master database directly so as to bypass publishing



Wednesday 8 May 2013

Finding friends in Spotify

Love Spotify but gotta say it's really dumb of them to come up with so good as the friend following feature but forget to include a way to find friends. oh well, for what is worth the functionality is there it's just the front enders that slacked on the job.

so until they implement a nice little user-friendly friend search function, if you wanna find someone on Spotify that you want to follow simply:

Go on the search box and type: spotify:user:USERNAME

obviously replace USERNAME with the name of the friend you're looking for.