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();
xslt.Load("output.xsl");

// 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:

_mainXml.AppendChild(root);
 
string allXmlStr = string.Empty;
using (var stringWriter = new StringWriter())
{
 
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
  {
    _mainXml.WriteTo(xmlTextWriter);
    xmlTextWriter.Flush();
    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.Load(xslReader);
      }
    }
    myXslTrans.Transform(xmlReader, null, resultSW);
    context.Response.Write(resultSW.GetStringBuilder().ToString()); 
  }
}
 
 

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:
http://gabrieleromanato.name/jquery-easy-table-pagination/
http://dotnetcluster.blogspot.co.il/2011/07/paging-in-html-table-using-javascript.html

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

i also added it to my Bresleveloper.js lib:
http://bresleveloper.blogspot.co.il/2013/06/my-extenxtion-javascript-namespace.html

<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;
    $table.trigger(
'repaginate');
  }).appendTo(pager);

  $(
'<span class="pg-goto"> « Prev </span>').bind('click', function () {
    if (currentPage > 0)
      currentPage--;
    $table.trigger(
'repaginate');
  }).appendTo(pager);

  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'];
        $table.trigger('repaginate');
    }).appendTo(pager);
  }

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

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

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

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

});

</script>