Pages

Tuesday, August 23, 2011

Remove xmlns from XML doc (C#) for use of X-Path

That's right!!!

I was using X-Path to read the nodes from an XML doc, but when I try doing it, it was always wrong.

Why? Because Microsoft Core XML, even the most recent one (you can see it here), only implements the X-Path 1.0 (since 1999).












And this X-Path version does not support the default namespace.

To overpass this problem, you can do 3 things:

  • Add the namespace to the XmlDocument and then use X-Path
  • Remove the namespace from the XmlDocument and then use X-Path
  • Use third dll's that implement newer versions of X-Path

There's some of solutions all over internet, but I prefer an generic solution, like removing all namespaces.

You can do it directly on XmlDocument or use regular expressions.

Personally, I like the regular expressions solution. Here it is:



This way you have an generic solution for all xml document and you can use X-Path with no worries.


Special Thanks to Pedro Lamas giving references and orientation.


References:
http://www.java2s.com/Code/CSharp/XML/Removeallthexmlnamespacesxmlnsattributesinthexmlstring.htm
http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c
http://www.eggheadcafe.com/community/aspnet/2/10021509/problem-with-xpath.aspx
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=3988

Monday, May 30, 2011

Clear Authentication Cache - Javascript

If we use http authentication, at the moment we want to logout a user for good, there's some issues we have to be aware of. So, we want to logout a user at client side and make sure that no one else has access to his/her account when using the same browser to access the same portal for instance. For IE, early versions, is pretty easy, we just do the code below:

// Clear current credentials
// Requires IE6 SP1 or later
document.execCommand(ClearAuthenticationCache)


Unfortunately, the ClearAuthenticationCache command is not available to others browsers in this case, so in order to do this really need to close the browser or, if it works for you, make an ajax call with wrong credentials to make your latest credentials saved to browser authentication cache be a 401 for that site. Mixing this all together we can have a javascript code like this one:




























References:
http://www.adopenstatic.com/cs/blogs/ken/archive/2005/04/12/14.aspx
http://stackoverflow.com/questions/31326/is-there-a-browser-equivalent-to-ies-clearauthenticationcache
http://stackoverflow.com/questions/1205045/how-to-clear-authentication-cache-on-ie7-with-javascript
http://www.nanodocumet.com/?p=6
http://msdn.microsoft.com/en-us/library/ms536979(v=vs.85).aspx
http://code.google.com/p/chromium/issues/detail?id=5497
http://trac-hacks.org/wiki/TrueHttpLogoutPatch

Saturday, May 28, 2011

Alter Sharepoint Timer Job Schedule - Object Model

If you want to alter a Sharepoint Timer Job Schedule without uninstall it, you can do it by an STSADM command like this one:
stsadm -o setcontentdeploymentjobschedule  -jobname "YourJobName"  -schedule "Weekly between Fri 22:00:00 and Sun 06:00:00"

But, this is only available if you are using MOSS. This stsadm command isn't available in WSS's, so in order to alter an timer job's schedule without going through all uninstall and install feature process, you can go by object model.

To simplify, we do a Console Application to comply our purpose. At main method...














And to finish we just do like this:































Done! ;)

References:
http://blogs.technet.com/b/josebda/archive/2008/03/15/complete-reference-of-all-stsadm-operations-with-parameters-in-moss-2007-sp1.aspx
http://social.technet.microsoft.com/Forums/en-US/sharepointadmin/thread/b29627df-ac9f-4eff-9d83-bf57a03848a8
http://www.codeguru.com/cpp/misc/misc/microsoftofficeoutlook/print.php/c14133

Tuesday, March 15, 2011

C# - XML Serialization of Class

Always useful!!! For whatever reason, it's always good to know how to serialize your class into a file, or any other kind of Stream. This example has one simple class with two properties, but it could be a global variable either, and the XmlSerializer usage.





















The result file of this example, looks like this:







Besides this, the serialization is not complete with the deserialization :) That's pretty simple to!








References:
http://support.microsoft.com/kb/815813/en-us

jQuery - Dynamic Table Creation Using Templates

Sometimes you really need to do things at client side, and one of them could be like a table creation with data on demand. Example, if you do a jQuery Ajax call, receive data in JSON format and present this data in a table, this is a pretty good and clean approach.

The example code goes like this:


































As you can see above, is simple and clean, even if you want to apply classes to <tr> or <td> tags for instance. The expected result from this code is a really dummy table.











References:
http://api.jquery.com/jquery.tmpl/

Wednesday, February 16, 2011

Post JSON to MVC Controller with XDomainRequest, XmlHttpRequest and jQuery Ajax Call

Once I try to post a JSON to a MVC controller, I use jQuery.post method and all works fine as you can see in this site, the first in the below references.

But when I try to use XDomainRequest or XmlHttpRequest, the JSON parameter at server side has null properties, in others words, the data is sent but not serialized to our serializable Type.

So, in order to receive data at server side, i used the DataContractJsonSerializer class to read the Stream sent in the Request, like this:





Of course, to make use of DataContractJsonSerializer, we need a class representing JSON with [DataContract] attribute and respective [DataMember] just like a WCF service for instance.

Then, the code at server side, looks like this:































And, at client side, making use of XDomainRequest and XmlHttpRequest Objects, and jquery the code is






















This way, you have a MCV controller that can receive JSON using the JSON serializing capacities of MCV3 from XDomainRequest, XmlHttpRequest and jQuery ajax call.


References:
http://lozanotek.com/blog/archive/2010/04/16/posting_json_data_to_mvc_controllers.aspx
http://msdn.microsoft.com/en-us/library/bb412170.aspx
http://msdn.microsoft.com/en-us/library/bb412179.aspx

Monday, February 14, 2011

Javascript - Consume Service With Cross Domain Request

Every time you need to do a request over your domain, you have to be aware of what kind of browser is running your code. If is an IExplorer, you will need to use the object XDomainRequest because this object is specifically to make cross-domain requests.

Here's a sample of usage:









If you want to make it complaint with others browsers you will need to use the XmlHttpRequest object, or, even simpler, making an ajax call with jQuery. This object, together with CORS, make possible to make cross domain requests.

The snippet of XmlHttpRequest goes like this:







And the call using jQuery:













XDomainRequest is the response by Microsoft against XmlHttpRequest with CORS protocol, instead of integrate it (CORS) with IExplorer, they create their own object for cross domain's requests.

Regarding CORS, all you have to do, is making your service server aware for OPTIONS verb and return the needed headers, because the browser itself makes the magic happens. So, to enable cross domain ajax call, at server you need to...



















With all this set up, you should have your communication with your server on. One little note, be aware of kind of service and object you are using, for instance, XDomainRequest only accept text/plain to send (xdr.send(string)) and an WCF service accepts XML, JSON, etc. but not text/plain.


References:
http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
http://stackoverflow.com/questions/4739384/xdomainrequest-problem
http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
http://www.codeproject.com/KB/ajax/jQueryWCFRest.aspx