Thursday, February 09, 2012

Using JQuery To Alter Sharepoint Toolbar Menu Item

So... I was trying to change the text of a menu option on a list toolbar in Sharepoint 2007 like this one...








The Purpose of this was to change the text on the menu without changing the content type name. This is really needed when you extend your sharepoint at the point that to change a content type name is not worthy!

After some search, I found a really simple way to do it! check it above.

//Get tag  and change it's attribute
$("ie\\:menuitem[text='Request']").attr("text", "Order");



References:
http://sharepoint.stackexchange.com/questions/2195/modification-of-layouts-upload-aspx

Friday, January 06, 2012

XSL transformation on XML document Using C#

Here is a pretty simple way for transforming an XML document using XSL through C#.

First you get your XML file, by string or file location using Load method, then using XmlTextWriter and XmlTextReader you apply the XSL and get the result on a StringBuilder... It seems cool right?

Now, what have bring you here... the code...


XmlDocument doc = new XmlDocument();
doc.LoadXml("Xml as a String");

StringBuilder resultString = new StringBuilder();
using (XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(resultString)))
{
    using (XmlTextReader xmlReader = new XmlTextReader(new StringReader(doc.OuterXml)))
    {
        string xslUrl = "http://YourHost/xsl/stylesheet.xsl";
        System.Xml.Xsl.XslCompiledTransform xslTransform = new System.Xml.Xsl.XslCompiledTransform();
        xslTransform.Load(xslUrl);
        xslTransform.Transform(xmlReader, xmlWriter);
    }
}
                
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(resultString.ToString());



Cheers!

References:
http://stackoverflow.com/questions/982600/net-xslt-transformation-is-this-really-streamed
http://msmvps.com/blogs/coad/archive/2004/04/13/4994.aspx
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
http://support.microsoft.com/kb/307322/en-us
http://www.codeproject.com/KB/cs/xsltrafo.aspx

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

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