Tuesday, July 23, 2013

how to use XslCompiledTransform, XSLT in c# (server side), and how to use it with Sharepoint

scenario 1: I just want to do XSLT in server side:
in most examples on the web u'll see stuff like this:

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();

// Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html");
now many times u'll and want to use the XmlReader and the XmlWriter classes, and there u'll start getting exceptions. The main reason is that those 2 classes really check for your XML, XSL and the output, so unless ur a goody good boy with XML-XSL its really annoying. One main example is that in my scenario I want to create a chunk of HTML but as a DIV, not a whole document, so you need to explicitly tell that to the result output.

so there r ways to continue and be a goody good boy and the only place I found good and exact info about all that is here: http://stackoverflow.com/questions/6214354/problem-with-xslcompiledtransform-and-xslt-in-vb-net/6214764#6214764, THX a lot u guys there!

BUT I chose to take the 1st answer and just skip the output checks by using the Trasform method with this signature:
StringWriter resultSW = new StringWriter();
myXslTrans.Transform(xmlReader, null, resultSW);

with this signature u skip the XmlWriter checks and just write strait into the stream.

scenario 2: SharePoint: they want me to refresh the content of my DataFormWebPart with ajax (yes they r stupid, but they pay), so I want to take my XSLT from the Style Library and in my case I had my XML custom made:

string allXmlStr = string.Empty;
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
    allXmlStr = stringWriter.GetStringBuilder().ToString();

using (XmlReader xmlReader = XmlReader.Create(new StringReader(allXmlStr)))
using (StringWriter resultSW = new StringWriter())
XslCompiledTransform myXslTrans = new XslCompiledTransform();
    string xsl = SPContext.Current.Site.RootWeb.GetFileAsString("/Style%20Library/[subFolderName]/Xsl/Channels.xsl");
    using (StringReader xslSR = new StringReader(xsl))
      using (XmlReader xslReader = XmlReader.Create(xslSR))
    myXslTrans.Transform(xmlReader, null, resultSW);

Thursday, July 18, 2013

Sharepoint ConnectionString for Jobs

THX to Elitzur and Eran for this one!!!

if u ever make a job in SP and in the job u want to get a connectionString from our beloved ConfigurationManager you will not find it!

this is becuz the OWSTIMER process (SharePoint Timer Service) has its own config file.

go to your 15\BIN (or 14\BIN) and find OWSTIMER.EXE.CONFIG and put ur connStr there.
dont forget to refresh the OWSTIMER.

some more info about jobs (for noobs like me XD):
jobs r running in the OWSTIMER process and therefor dont have any context(es) with them, so you cant cache stuff (my original will), you dont have ur web.config and other IIS related stuff.

Wednesday, July 3, 2013

Html Table Pagination: Real True and Complete (and Simple) Javascript with JQuery

i did a mixture and optimization from these 2:

now it has <<first, <<prev, 5,6,7,8,9 next>> last>>

i also added it to my Bresleveloper.js lib:

<script type="text/javascript">
$(function () {
$('table.paginate').each(function () {
var $table = $(this);
var itemsPerPage = 10;
var currentPage = 0;
var pages = Math.ceil($table.find("tr:not(:has(th))").length / itemsPerPage);
$table.bind('repaginate', function () {
  if (pages > 1) {
  var pager;
  if ($table.next().hasClass("pager"))
    pager = $table.next().empty();  else
  pager = $('<div class="pager" style="padding-top: 20px; direction:ltr; " align="center"></div>');

  $('<span class="pg-goto"></span>').text(' « First ').bind('click', function () {
    currentPage = 0;

'<span class="pg-goto"> « Prev </span>').bind('click', function () {
    if (currentPage > 0)

  var startPager = currentPage > 2 ? currentPage - 2 : 0;
  var endPager = startPager > 0 ? currentPage + 3 : 5;
  if (endPager > pages) {
    endPager = pages;
    startPager = pages - 5;
    if (startPager < 0)
      startPager = 0;

  for (var page = startPager; page < endPager; page++) {
    $('<span id="pg' + page + '" class="' + (page == currentPage ? 'pg-selected' : 'pg-normal') + '"></span>').text(page + 1).bind('click', {
        newPage: page
function (event) {
        currentPage = event.data['newPage'];

'<span class="pg-goto"> Next » </span>').bind('click', function () {
    if (currentPage < pages - 1)
  $('<span class="pg-goto"> Last » </span>').bind('click', function () {
    currentPage = pages - 1;

  if (!$table.next().hasClass("pager"))
// end $table.bind('repaginate', function () { ...

'tbody tr:not(:has(th))').hide().slice(currentPage * itemsPerPage, (currentPage + 1) * itemsPerPage).show();

});// end $('table.paginate').each(function () { ...