Log files and TextWriterTraceListener

February 1, 2012 2 comments

Writing a log file using TraceListner classes its just one more way of writing logs..ย  ๐Ÿ™‚

private int Logger(Exception exObj)
{
using (Stream myFile = File.Exists(“TestFile.txt”) ? File.Open(“TestFile.txt”, FileMode.Append) : File.Create(“TestFile.txt”))
{
/* Create a new text writer using the output stream, and add it to * the trace listeners. */
TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);
// Write output to the file.
Trace.Write(“\r\n Test output \r\n”);
Trace.Write(exObj);
Trace.Write(“\r\n Test output \r\n”);

// Flush the output.
Trace.Flush();
myFile.Close();

Trace.Listeners.Remove(myTextListener); // this is very important else you may encounter error wrt file/stream/trace object
myFile.Dispose();
}
return 0;
}

Categories: General Tags: , ,

Get dataset from xml document using XmlNodeReader

February 1, 2012 1 comment

Hi, many times we need to read an xml file and get it in either dataset or datatable. This is ok till we directly use dataset.readXML method, but what when we just need to read specific nodes (infact elements) and then create dataset/datatable out-of it? (quite tricky right)

The answer is create an XmlNodeReader object and get the required element (node) into it either by traversing the xml document or by firing a simple (yet powerful) XPath Query on to the document.ย  The below sample explains the same by making use of Xpath Query.

private void btnGetData_Click(object sender, EventArgs e)
{
//Create xmlnode and xmlElement for xpath
XmlNode xmlNode;
XmlElement root = doc.DocumentElement;

// xpath expression //List[@Name=’Lotus Machines’]
xmlNode = root.SelectSingleNode(txtExpression.Text);

DataSet dataSet = new DataSet();
if (xmlNode.HasChildNodes)
{
for (int indexNode = 0; indexNode < xmlNode.ChildNodes[0].ChildNodes.Count; indexNode++)
{
XmlNode innerNode = xmlNode.ChildNodes[0].ChildNodes[indexNode];
using (XmlNodeReader reader = new XmlNodeReader(innerNode))
{// fill dataset from reader object as below ๐Ÿ™‚
dataSet.ReadXml(reader);
}
}
}
}

I would definitely like to hear from ur side if there is any other better way for so… till the bye byeย  ๐Ÿ™‚

2011 in review

January 7, 2012 Leave a comment

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

A New York City subway train holds 1,200 people. This blog was viewed about 5,300 times in 2011. If it were a NYC subway train, it would take about 4 trips to carry that many people.

Click here to see the complete report.

Categories: General

Heavy shelling with powershell

August 10, 2011 Leave a comment

Hi,
in this post i’m going to cover the powershell as what i understood. hope you all find it appropriate and worth for your work. if not than please do feel free to update / upgrade my knowledge :).

So starting with shelling ๐Ÿ™‚

what is powershell:
* windows powershell is microsofts task automation framework [as per wikipedia] built primarily for the windows administrators.
* It basically consist of command-line shell which is build on top of .net common language runtime and .net framework that accepts and returns .NET framework objects unlike to its counterparts which accept and return text.
* An administrator can perform administrative tasks on local but also on remote windows system through the virtue of powershell support to COM and WMI

As per microsoft its: http://msdn.microsoft.com/en-us/library/aa973757%28VS.85%29.aspx

I came across some handy tool / editor for playing with the powershell at http://powergui.orgย  do check it.

Regards,
Manish Patil

Categories: General Tags:

Manipulating SharePoint Lookup Field values using Javascript

May 31, 2011 1 comment

This article gives an small trick for manipulating the values of SharePoint Lookup Field (OOB control for list item lookup). For one of my assignments I was required to manipulate the values listed into SharePoint Lookup Field so I did it in this fashion ๐Ÿ™‚

function getField(fieldType,fieldTitle)
{
var docTags = document.getElementsByTagName(fieldType);
for (var i=0; i < docTags.length; i++)
{
if (docTags[i].title == fieldTitle)
{
docTags[i].readOnly = true;
docTags[i].setAttribute(“choices”,”Comment 1|20″);
}
}
return false;
}
getField(‘input’,’Comment Type’);

Categories: General Tags:

Add .net assembly to sharepoint 2010 package or solution

April 5, 2011 Leave a comment

hi,

here in today I would like to share a small but required thing while packaging sharepoint solutions. We usually require to bundle-up .net assemblies within a sharepoint solution(s). Microsoft Visual studio 2010 has made this thing very easy for packaging sharepoint 2010 solutions.

Please check the screen shots for the sameIn the solution explorer you can see an .net assembly( underlined red in color), I wanted to refer/add this assembly into the sharepoint solution (highlighted in dark black).

So I opened the package.package (double click it ๐Ÿ™‚ ) as shown in screen shot.

Here on the wizard click on Advance tab (highlighted in red box below)

On opening of the tab gives option (inform of buttons) to add, edit, delete “Additional Assemblies”. The added assemblies are listed in the grid.

When we try to add new assembly it will give 2 options viz; Add existing assembly & Add assembly from project output

Now if you want to add assembly from your current project/solution then use second option & if you to refer to external assembly which is not part of your project then use first option.

In either cases if you want to register these assemblies into the web.config safe controls then there is provision for same ๐Ÿ™‚

One most important thing to remember is: any assembly that you want to refer/add should be strong named (public token key) in order to add it to the GAC / assembly cache.

Additional Resource can be found at: Add & Remove Additional Assemblies

Get SPweb, SPList or Document Library from SPContentDatabase Class

April 5, 2011 1 comment

Hi,

This post is helpful for those who are trying to extract information from the SPContentDatabase class. This is usually required when one is creating custom timer job, base level features etc.

The following code is written for Custom timer job:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration; (must)
using Microsoft.SharePoint;

namespace TTKTimerJob
{

class TTKIssueTimerJob: SPJobDefinition
{

public TTKIssueTimerJob()
: base()
{ }

public TTKIssueTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{ }

public TTKIssueTimerJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = “TTK Issue Timer Job”;
}

public override void Execute(Guid targetInstanceId)
{

// get a reference to the current site collection’s content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
// Get the TimerJobList Name from the webApplication collection
SPSiteCollection timerSiteCollection = webApplication.ContentDatabases[targetInstanceId].Sites;
SPList timerJobList = null;
foreach (SPSite siteItem in timerSiteCollection)
{
#region code here: finds the List in rootweb
//assumes the existence of a site collection with an absolute URL of
//http://localhost/sites/sitecollection/subsite/Lists/abc and that this site collection has a Web site named subsite
//SPList xyz = siteItem.RootWeb.GetList(“list url here”);

//SPList xyz = siteItem.RootWeb.Lists.TryGetList(“list Title here”);
timerJobList = siteItem.RootWeb.Lists.TryGetList(“ListTimerJob”);
if (timerJobList != null)
{
// create a new list Item, set the Title to the current day/time, and update the item
SPListItem newList = timerJobList.Items.Add();
newList[“Title”] = DateTime.Now.ToString();
newList.Update();
break;
}
#endregion}

}

}

}