Tuesday, April 19, 2011

Using WebGrid in ASP.NET MVC3


@model IEnumerable

@{
ViewBag.Title = "Employee Grid";
}

@{
var grid = new WebGrid(source: Model, defaultSort:"Name", rowsPerPage:3);
}

Employee Grid



@grid.GetHtml(columns: grid.Columns(grid.Column("Id"), grid.Column("Name"), grid.Column("JoiningDate"), grid.Column("PAN")))

MVC Application Execution Process (C#)


  1. When Receive first request for the application-
    In the Global.asax file, Route objects are added to the RouteTable object
  2. Perform routing -
    The UrlRoutingModule
    module uses the first matching Route object in the RouteTable
    collection to create the RouteData object, which it then uses
    to create a RequestContext (IHttpContext) object.
  3. Create MVC request handler -
    The MvcRouteHandler object creates an instance of the MvcHandler class and passes it the RequestContext instance.
  4. Create controller -
    The MvcHandler object uses the RequestContext instance to identify the IControllerFactory object (typically an instance of the DefaultControllerFactory class) to create the controller instance with.
  5. Execute controller -
    The MvcHandler instance
    calls the controller s Execute method.
  6. Invoke action -
    Most controllers inherit from the Controller base class. For controllers that do so, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and
    then calls that method.
  7. Execute result -
    A typical action method might receive user input, prepare the appropriate response data, and then execute the result by returning a result type. The built-in result types that can be executed include the following: ViewResult (which
    renders a view and is the most-often used result type), RedirectToRouteResult,
    RedirectResult
    , ContentResult, JsonResult, and
    EmptyResult
    .

Friday, April 15, 2011

Extract WSP from SharePoint Solution Store


using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

static void Main(string[] args)
{
const string extractingTo = "e:\\Solutions\\";
SPSolutionCollection solutions = SPFarm.Local.Solutions;
foreach (SPSolution solution in solutions)
{
Console.WriteLine("Extracting " + solution.Name);
SPPersistedFile wspFile = solution.SolutionFile;
wspFile.SaveAs(extractingTo + solution.Name);
}
}

System.Data.SqlClient.SqlException while installing SPJob during Feature activation

Recently we came across following exception while installing SPJob during Feature installation. System.Data.SqlClient.SqlException: The EXECUTE permission was denied on the object 'proc_putObject', database 'SharePoint_Config', schema 'dbo'.

Reason: The scope of the feature was "Web". In fact the same exception will occure if you define the scope of the feature as "Site".

Solution: Define the scope of the feature as "Web Application".

Monday, April 11, 2011

Edit Header/Footer of Word Document programmatically using Office Open XML

Scenario- My Client wants to publish policy document(Word Document) to multiple departments with department specific header and footer.

Design-
System.IO.Packing API provides strongly typed classes to manipulate office documents that adhere to office open XML file format specification. An open XML document like Word Document is stored as a package. The package has multiple parts like main document, header, footer etc. with relationship between them. The header part is included in the document by header reference tag. Changing the header information of a Word Document is to delete the existing header part and add a new header part.

Solution-

public SPFile ProcessHeaderFooter(string targetFilePath, string department)
{
string headerInfo = GetHeaderInfo(department);
string footerInfo = GetFooterInfo(department);

Stream headerStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(headerContent));
Stream footerStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes (footerContent));

SPWeb web = SpContext.Current.Web;
SPFile targetFile = web.GetFile(targetFilePath);
MemoryStream fileStream = new MemoryStream();
fileStream.Write(targetFile.OpenBinary(), 0, (int)targetFile.TotalLength);
AddHeader(headerStream, fileStream);
AddFooter(footerStream, fileStream);
targetFile.SaveBinary(fileStream);
fileStream.Close();
return targetFile;
}

public void AddHeader(Stream headerContent, Stream fileContent)
{
const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
const string headerContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml";
const string headerRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header";
const string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
PackagePart documentPart = null;
using (Package wdPackage = Package.Open(fileContent, FileMode.Open, FileAccess.ReadWrite))
{
// Get the main document part (document.xml).
foreach (System.IO.Packaging.PackageRelationship relationship in wdPackage.GetRelationshipsByType(documentRelationshipType))
{
Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
documentPart = wdPackage.GetPart(documentUri);
// There is only one officeDocument.
break;
}

Uri uriHeader = new Uri("/word/header1.xml", UriKind.Relative);

if (wdPackage.PartExists(uriHeader))
{
wdPackage.DeletePart(uriHeader);
}

// Create the header part.
PackagePart headerPart = wdPackage.CreatePart(uriHeader, headerContentType);
XmlDocument headerDoc = new XmlDocument();
headerContent.Position = 0;
headerDoc.Load(headerContent);

// Write the header out to its part.
headerDoc.Save(headerPart.GetStream());

// Create the document's relationship to the new part.
PackageRelationship rel = documentPart.CreateRelationship(uriHeader, TargetMode.Internal, headerRelationshipType);
string relID = rel.Id;

// Manage namespaces to perform Xml XPath queries.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);

// Get the document part from the package.
// Load the XML in the part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(documentPart.GetStream());

// Find the node containing the document layout.
XmlNode targetNode = xdoc.SelectSingleNode("//w:sectPr", nsManager);
if (targetNode != null)
{
// Delete any existing references to headers.
XmlNodeList headerNodes = targetNode.SelectNodes("./w:headerReference", nsManager);
foreach (System.Xml.XmlNode headerNode in headerNodes)
{
targetNode.RemoveChild(headerNode);
}

// Create the new header reference node.
XmlElement node = xdoc.CreateElement("w:headerReference", wordmlNamespace);
XmlAttribute attr = node.Attributes.Append(xdoc.CreateAttribute("r:id", relationshipNamespace));
attr.Value = relID;
node.Attributes.Append(attr);
targetNode.InsertBefore(node, targetNode.FirstChild);
}

// Save the document XML back to its part.
xdoc.Save(documentPart.GetStream(FileMode.Create, FileAccess.Write));
}
}


public void AddFooter(Stream footerContent, Stream fileContent)
{
const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
const string footerContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml";
const string footerRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer";
const string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
PackagePart documentPart = null;
using (Package wdPackage = Package.Open(fileContent, FileMode.Open, FileAccess.ReadWrite))
{
// Get the main document part (document.xml).
foreach (System.IO.Packaging.PackageRelationship relationship in wdPackage.GetRelationshipsByType(documentRelationshipType))
{
Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
documentPart = wdPackage.GetPart(documentUri);
// There is only one officeDocument.
break;
}

Uri uriFooter = new Uri("/word/footer1.xml", UriKind.Relative);
if (wdPackage.PartExists(uriFooter))
{
// Although you can delete the relationship
// to the existing node, the next time you save
// the document after making changes, Word
// will delete the relationship.
wdPackage.DeletePart(uriFooter);
}
// Create the footer part.
PackagePart footerPart = wdPackage.CreatePart(uriFooter, footerContentType);

XmlDocument footerDoc = new XmlDocument();
footerContent.Position = 0;
footerDoc.Load(footerContent);
// Write the footer out to its part.
footerDoc.Save(footerPart.GetStream());
// Create the document's relationship to the new part.
PackageRelationship rel = documentPart.CreateRelationship(uriFooter, TargetMode.Internal, footerRelationshipType);
string relID = rel.Id;

// Manage namespaces to perform Xml XPath queries.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);

// Get the document part from the package.
// Load the XML in the part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(documentPart.GetStream());

// Find the node containing the document layout.
XmlNode targetNode = xdoc.SelectSingleNode("//w:sectPr", nsManager);
if (targetNode != null)
{
// Delete any existing references to footers.
XmlNodeList footerNodes = targetNode.SelectNodes("./w:footerReference", nsManager);
foreach (System.Xml.XmlNode footerNode in footerNodes)
{
targetNode.RemoveChild(footerNode);
}

// Create the new footer reference node.
XmlElement node = xdoc.CreateElement("w:footerReference", wordmlNamespace);
XmlAttribute attr = node.Attributes.Append(xdoc.CreateAttribute("r:id", relationshipNamespace));
attr.Value = relID;
node.Attributes.Append(attr);
targetNode.InsertBefore(node, targetNode.FirstChild);
}

// Save the document XML back to its part.
xdoc.Save(documentPart.GetStream(FileMode.Create, FileAccess.Write));
}
}


References
http://msdn.microsoft.com/en-us/library/bb491088(v=office.12).aspx

Tuesday, April 5, 2011

Impersonating using SPUserToken object


using (SPSite site = new SPSite(siteURL))
{
using (SPSite siteAsOtherUser = new SPSite(siteURL, site.RootWeb.AllUsers["WS2003\\Administrator"].UserToken))
{
SPWeb web = siteAsOtherUser.OpenWeb();
//Write code with elevated privilege.
}
}

Type of users in SharePoint

Users: Users explicitly added to the SPWeb.
AllUsers: Users explicitly added to the SPWeb + Users got access implicitly thorough groups of SPWeb.
SiteUsers: Aggregation of all Users at the SiteCollection level.