Getting started with Microsoft Tag
Written by Mike James   
Monday, 07 June 2010
Article Index
Getting started with Microsoft Tag
Creating and editing tags
Working with image data

Create a tag

The first thing we have to learn how to do is create a tag.  It is important to realise that what is happening is that you are simply creating an entry in the Tag Manager - the same one you use when manually creating a Tag.

Banner

To get started place a button on the form and add:

using WpfApplication1.MicrosoftTag;

or what ever your application's namespace is called and what ever namespace you applied to the generated tag classes.

We need to first create and initialize a Tag object - in this case a URITag object. There are classes for each category of Tag you can create.

URITag URItag = new URITag();

The tag object has lots of properties that we could set but  at a minimum we have to set its Title, URL and start date:

URItag.MedFiUrl = 
"http:///wwww.i-programmer.info";
URItag.Title = "MyTag";
URItag.UTCStartDate = DateTime.Now;

This is sufficient to define the Tag we are about to create. If you leave the UTCEndDate property undefined then the tag never expires. If you do specify a time period then it must be valid or an exception is generated.

To do this we need a client object that can make calls to the web service:

MIBPContractClient Client = 
new MIBPContractClient();

we also need to pass a UserCredential object to specify the API Key:

UserCredential Creds = 
new UserCredential();
Creds.AccessToken = "YOUR KEY";

Now all is ready to make the actual service call and create the Tag in the Tag Manager:

Client.CreateTag(Creds,"Main",URItag);

This creates a Tag as specified by the tag object in the "Main" category in the Tag Manager using the supplied credentials.

There are many things that can go wrong whenever you call a web service so in a real application you need to surround everything with a try-catch and work out what to do when it does all go wrong. Notice that you can only create a tag with a given title once so even if this program works the first time it will fail the second time you run it.

If you run the program and navigate to the Tag Manager then you will see the new Tag listed. You can now abandon the API if you want to and proceed to manage and render the Tag using the Tag Manager.

Editing

In the same way that you just created a Tag you can change the properties of existing Tag by simply creating a Tag object with the new properties set and then use the UpdateTag method. Notice that Tags are identified by category and title. You can change every Tag property using the Update but you also have to specify essential properties that aren't changing. For example, you have to specify the start date even if this isn't changing.

For example to change the Tag created earlier so that it has a new name and a new URL we would use something like:

URITag URItag = new URITag();
URItag.Title = "MyTagNews";
URItag.MedFiUrl =
"http:///wwww.i-programmer.info/news";
URItag.UTCStartDate = DateTime.Now;

Notice that we have to set the start date even if we don't want to change it. Now we have the details fo the tag setup we can use the Update method to save it:

MIBPContractClient Client = 
new MIBPContractClient();
UserCredential Creds =
new UserCredential();
Creds.AccessToken = "YOUR KEY";
Client.UpdateTag(
Creds, "Main","MyTag", URItag);

The Update method identifies the Tag to be changed by the category and title. The update will fail if the original Tag doesn't exist or if the updated Tag already exists. Notice that this means that running this program twice will fail. However you can update the same Tag as many times as you like i.e. without changing its name.

Also notice that in a real application you generally wouldn't create the Client object each time you needed to access the web service and, of course, you would add some error handling.

Currently there are some big omissions from the API. There is no way to download a list of Tags that already exist. You can't delete a Tag. You can't access the details fo a Tag already created nor can you find out about categories or the statistics associated with a Tag. There are ways around some of these limitations - for example you can keep your own database of Tags created - but it to be hoped that the next version  of the API adds these facilities.

As well as updating tags you can also pause a Tag or a complete category. You can also create and update Categories. If you have a lot of Tags to work with then clearly defining and using a sensible set of categories is a good idea.

Banner

<ASIN:1847196624>

<ASIN:0321440064>

<ASIN:0596510373>

<ASIN:0596519206>

<ASIN:0596101627>

<ASIN:1890774464>



Last Updated ( Wednesday, 04 August 2010 )