Wednesday, May 04, 2011

Downloading the SharePoint Document

Recently,a strange scenario in SharePoint 2010 development I faced.
I created a Visual WebPart with sandbox solution based to let user download the files from the SharePoint Document Library.
Labour the lines of code to get reference of the SPFile from the current web context.
Reading and Writing the file contents into streams to let user to download the file as soon as they clicked it on the "Download" link.It will prompts the "File Download Dialog box".
I deployed it on the sharepoint development environment it just works fine.
Later,I came to know that webpart is not allowing the end user to download.
Again I checked the same webpart performance and put optimised looping and disposing the objects verily stated manner.
Its works fine as I expected.But When I deployed it on SharePoint Server,I faced the same problem when user accessing the page.
When end user clicks the link "Download",Browser redirect to "Forbidden page".

Finally I have concluded the user is only "Reader" permission group.

Simply added RunWithElevatedPrivileges
Its works fine for all user group permission.
protected void Button1_Click(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
FileAction();
});
}
protected void FileAction()
{
SPWeb site = SPControl.GetContextWeb(Context);
SPFile spFile = site.GetFile(site.Url + "/DocLibrary/" + "1.txt");
// Above line for clarity purpose hard coded.You can read the file name from the //library.My requirement there was only one file.
FileStream fs = new FileStream("C:\\Windows\\Temp\\tempContents.txt", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(spFile.OpenBinary());
bw.Close();
fs.Close();

using (FileStream sm = File.Open("C:\\Windows\\Temp\\tempContents.txt", FileMode.Open))
 {
using (StreamReader sr = new StreamReader(sm))
{
Response.AppendHeader("Content-Disposition", "attachment; filename =" + spFile.Name);
Response.ContentType = "text/plain";
using (StreamWriter sw = new StreamWriter(Response.OutputStream))
{
sw.Write(sr.ReadToEnd());
}
}
sm.Close();
Response.Flush();
Response.Close();
}
}

Wednesday, April 20, 2011

decimal control in double value

There were situation double values are to be controlled only to 3 decimal or 2 decimal.
This one line code does the exact function.
        double d = 42.566454648;
        double d1 = 23.545548125;
        double d2 = d + d1;
        Response.Write(d2.ToString("N3"));

Monday, April 11, 2011

two column filtering - AND CAML

Filtering the rows on two column condition on CAML query





India




Chennai




I have a sharepoint list contains the columns
Country | City | Price1 | Price2|
Records will be
India | Chennai | 254.3 | 536.5
UAE | Dubai | 500.24| 65.21

Thursday, February 24, 2011

MVC2 Template Helpers

Templated Helpers

In ASP.NET MVC 2.0 newly added feature is Templated Helpers.This allows you to create display screens and
editable screens.
It strongly supports the refactoring,intellisense and compile time checking.
<%= Html.DisplayFor(m => m.Name) %>
Alternatively you can try the
Html.EditorFor()
<%= Html.LabelFor(m =>m.Name) %>