Dear Friends , here I am having problem with visual web part.After deploying into SharePoint it is not accessible for all users.It is accessible for only who created.When I try to log in with another user this webpart is not working .I used RunWithElevatedPrivileges method also,even though it is not working.Please help me to fix this issue.
here is is my complete code please check it and sort out my issue
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.Utilities;
namespace BursaryVisualWebpart.BursaryCaseID_DocumentsVisualWebpart
{
public partial class BursaryCaseID_DocumentsVisualWebpartUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
string bursaryapp = "BURSARY/2012/003";
SPList document = web.Lists["Requirement"];
SPQuery query = new SPQuery();
query.Query = "" + bursaryapp + " ";
SPListItemCollection objItemcoll = document.GetItems(query);
DataTable itemsDataTable = objItemcoll.GetDataTable();
GridView1.DataSource = itemsDataTable;
GridView1.DataBind();
}
}
});
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton hypLink = (LinkButton)e.Row.Cells[3].Controls[1];
hypLink.CommandArgument = e.Row.RowIndex.ToString();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "download")
{
string filename = e.CommandArgument.ToString();
GridView gv = this.GridView1;
HyperLink hypLink = (HyperLink)gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Controls[1];
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
string url = web.Url + hypLink.NavigateUrl;
SPFile tempFile = web.GetFile(url);
byte[] obj = (byte[])tempFile.OpenBinary();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.AddHeader("Content-Type", "Application/octet-stream");
Response.AddHeader("Content-Length", obj.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + hypLink.Text);
Response.BinaryWrite(obj);
Response.Flush();
Response.End();
web.AllowUnsafeUpdates = false;
}
}
catch (Exception ex)
{
SPContext.Current.Web.AllowUnsafeUpdates = false;
}
}
}
}
..........................................................................................
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BursaryCaseID_DocumentsVisualWebpartUserControl.ascx.cs" Inherits="BursaryVisualWebpart.BursaryCaseID_DocumentsVisualWebpart.BursaryCaseID_DocumentsVisualWebpartUserControl" %>
function setFormSubmitToFalse() {
if (_spFormOnSubmitCalled) {
_spFormOnSubmitCalled = false;
}
}
EnableModelValidation="True" onrowcommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated">
NavigateUrl='<%# Eval("FileLeafRef", "/Requirement/{0}") %>' Target="_parent"
Text='<%# Eval("FileLeafRef") %>' >
<%-- --%>

Adil Ansari149 reputationPosted Aug 28, 2012, 1:35 AM EDT
Is it not at all getting loaded/render for other user or gives error when you click on hyperlink in grid for download?
I guess you also need to add the SPSecurity.RunWithElevatedPrivileges in protected void GridView1_RowCommand while downaloding, we are directly accessing the SPContext and downloading the file it may give error it other user don't have the proper permission on it
Just check if it works for you and if not please elaborate more on error
Have fun with SharePoint