Pages

Wednesday, October 26, 2011

Sharepoint Timer Job in One Server Only Chosen By You

Ok, this is one scenario that can occur many times! For instance, you have a farm with 4 servers and you want to deploy one Sharepoint Timer Job with site collection scope but you need it to run in an specific server (whatever reasons you have). If you do not indicate what server to run, the installation will install the job in the server that is the admin one (not pretty sure on this one) or, ultimately you will have the same job replicated in every server.

You can see a very good base example how to build an sharepoint timer job here (Andrew Connell Blog), and I will focus this post on how to chose a server to run, not how to install it.

So, to do this, and taking the Andrew Connell's example, you have to do three things:

  • Add a new constructor at SPJobDefinition
  • Get the server you want (SPServer instance)
  • Use the new constructor that implements a different SPJobLockType regarding the example taken

At this time, you are already thinking, "come on!!! some me some code!!!" here it is...

At class that is inheriting the SPJobDefinition you add this constructor
Than, at FeatureActivated method in the TaskLoggerJobInstaller class (the one that inherits from SPFeatureReceiver) you get the server instance you want the job get up and running and use the new constructor added above
























As you can see, you can get the SPServer only by his name (Const.SERVER is a string with server name). But the key here, is the SPJobLockType because you have three choices to use it and to guarantee that it gets register in specific server you chose Job option as you can see in the new constructor. The next image is very self explanatory.













In conclusion, with three simple steps, we can centralize our timer jobs in one specific server making use of the option Job of SPJobLockType and SPServer together. Some warnings... Do not try coding the SPServer with SPJobLockType ContentDatabase, it will fail because you can not do it. On the Execute method, regarding the example, do not use the SPContentDatabase to get the SPSite, use the SPWebApplication, something like this SPSite site = webApplication.Sites[SiteUrl];

References:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spjobdefinition_members.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spjoblocktype.aspx
http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx
http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/8a9b9054-ce04-4536-a537-1b2a4f34ca72

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

Friday, February 04, 2011

WIF - "Thread was being aborted" Error When SignIn / SignOut

If you ever try to implement WIF (code name Geneva), you probably found the error "Thread was being aborted" at the moment you try to execute the:

FederatedPassiveSecurityTokenServiceOperations.ProcessSignInResponse(responseMessage, response);
OR
FederatedPassiveSecurityTokenServiceOperations.ProcessSignOutRequest(requestMessage, User, requestMessage.Reply, Response);

This happens because in the middle of Windows.IdentityModel there's an Response.Redirect("returned page") instead of Response.Redirect("returned page", false) causing the error above mentioned. This is an known issue, and is save to ignore it. So, making use of a catch, you can jump the error and your code would be like...












You can skip this error and hoping for an hotfix soon :)


References:
http://p2p.wrox.com/asp-net-1-1/8403-error-thread-being-aborted.html
http://rmencia.wordpress.com/2010/05/05/federated-signin-requires-federated-signout/
http://social.msdn.microsoft.com/Forums/en/Geneva/thread/b248fafd-f5aa-47f7-9c7a-1ecaa944d2c0

Wednesday, February 02, 2011

C# - Get All Properties From Class Or Type [Reviewed]

Looking to the previous post, with an excellent advice from Pedro Lamas (Thanks a lot!!!), the source code now goes like this...





















Now, making use of reflection and extension method together, we have the possibility to use the GetPropertiesAndValues method at any object inside the program scope.

In other words, you can use GetPropertiesAndValues for every object (like an instance of a class) present in your program, getting the returned string "My object Properties & Values \n\nPropName: PropValue\n (etc.)"

For instance:



















References:
http://stackoverflow.com/questions/1447308/enumerating-through-an-objects-properties-string-in-c
http://www.developer.com/net/csharp/article.php/3592216/Using-the-New-Extension-Methods-Feature-in-C-30.htm

Tuesday, February 01, 2011

C# - Get All Properties From Class Or Type

Have you ever try to get all properties from a Class/Type on the fly? the next snippet demonstrates how to query the class instance and retrieve the properties names and their respective values.

It goes like this...




















This way you have a string returned like "propName1: valueProp1\n" and so on.


Reference:

Monday, January 31, 2011

Javascript - Catch Unhandled Exceptions

Have you ever thought to manage all exceptions at client side? if you use a simple line of code, like the one presented below, you can get an simple message and direct you to the right way to solve the script problem.










The window.onerror is an event handler for errors events sent to the window, catching all Unhandled Exceptions. But this approach has the problem that only works on IE or Gecko based browsers. It's a damn limitation for sure...

BTW, in order to test the onerror event, use a simple code line like "color[2]" without any initializing letting the script get error, then you'll see the alert window.



References:
https://developer.mozilla.org/En/DOM/Window.onerror
http://stackoverflow.com/questions/339580/does-javascript-fire-an-event-for-unhandled-uncaught-exceptions
http://stackoverflow.com/questions/1008692/window-onerror-not-firing-in-firefox