Problem with Custom Web Part! Big issue!
Ok, so I can use this web part and it adds to the list just fine, but when my colleague with the same permissions to the list uses it, she can't get it to add to the list.... I implemented the run with elevated priviledges and used guids instead of using the spcontext.current(heard that negates the elevated priviledges) Any thoughts?
private void PopulateDataset()
{
try
{
mySite = new SPSite(siteURL);
try
{
myWeb = mySite.OpenWeb(parentURL);
}
catch
{
myWeb = mySite.RootWeb;
}
favList = myWeb.Lists[listName];
NAPWS.Service napService = new global::WPRecordSiteVisits.NAPWS.Service();
oDataSet = napService.SearchClients();
oView.Table = oDataSet.Tables[0];
}
catch(Exception e){
lblError.Text += "| PopulateDataSet: " + e.Message;
}
}
private void RecordSiteVisits()
{
try{
if ((HttpContext.Current.Request.UrlReferrer == null || HttpContext.Current.Request.UrlReferrer.AbsoluteUri.IndexOf(SPContext.Current.Web.Url) == -1) && (HttpContext.Current.Session["Referrer"] == null || HttpContext.Current.Session["Referrer"].ToString().IndexOf(SPContext.Current.Web.Url) == -1))
{
if (HttpContext.Current.Session["Referrer"] != null)
HttpContext.Current.Session["Referrer"] = SPContext.Current.Web.Url;
string pathString = HttpContext.Current.Request.Url.AbsolutePath.ToString();
string strNC_Number = "";
if (pathString.IndexOf("NAP/") != -1)
{
pathString = pathString.Substring((pathString.IndexOf("NAP/") + 4));
strNC_Number = pathString.Split(new String[] { "/" }, StringSplitOptions.None)[0];
}
SPQuery newQuery = new SPQuery();
strNC_Number = "346";
int nc_Number = Convert.ToInt32(strNC_Number);
newQuery.Query = "<Where><Eq><FieldRef Name=\"NC_NUMBER\"/><Value Type=\"Text\">" + nc_Number + "</Value></Eq></Where>";
SPListItemCollection thisCollection = favList.GetItems(newQuery);
try
{
if (thisCollection.Count == 0)
{
SPListItem newItem = myWeb.Lists[listName].Items.Add();
oView.RowFilter = "NC_NUMBER = '" + nc_Number + "'";
newItem["NC_NAME"] = oView.ToTable().Rows[0]["NC_NAME"];
newItem["NC_NUMBER"] = nc_Number;
SPSite mySite = new SPSite(SPSiteGuid);
SPWeb myRootSite = mySite.OpenWeb(SPWebGuid);
//SPWeb myRootSite = SPContext.Current.Web;
String login = myRootSite.CurrentUser.LoginName;
newItem["UserLoginWithDomain"] = login;
newItem["Category"] = "Automatic";
newItem["Site Visits"] = 1;
myWeb.AllowUnsafeUpdates = true;
newItem.Update();
myWeb.AllowUnsafeUpdates = false;
}
else if (thisCollection.Count == 1)
{
SPListItem tmpItem = thisCollection[0];
tmpItem["Site Visits"] = Convert.ToInt32(tmpItem["Site Visits"]) + 1;
myWeb.AllowUnsafeUpdates = true;
tmpItem.Update();
myWeb.AllowUnsafeUpdates = false;
}
else
{
lblError.Text += "Duplicate Records exist in your my favorites list for the national client";
}
}
catch (Exception error)
{
lblError.Text += " |RecordSiteVisits: "+error.Message;
}
}
}
catch (Exception error2)
{
lblError.Text += error2.Message;
}
}
private void RecordSiteVisits()
{
try{
if ((HttpContext.Current.Request.UrlReferrer == null || HttpContext.Current.Request.UrlReferrer.AbsoluteUri.IndexOf(SPContext.Current.Web.Url) == -1) && (HttpContext.Current.Session["Referrer"] == null || HttpContext.Current.Session["Referrer"].ToString().IndexOf(SPContext.Current.Web.Url) == -1))
{
if (HttpContext.Current.Session["Referrer"] != null)
HttpContext.Current.Session["Referrer"] = SPContext.Current.Web.Url;
string pathString = HttpContext.Current.Request.Url.AbsolutePath.ToString();
string strNC_Number = "";
if (pathString.IndexOf("NAP/") != -1)
{
pathString = pathString.Substring((pathString.IndexOf("NAP/") + 4));
strNC_Number = pathString.Split(new String[] { "/" }, StringSplitOptions.None)[0];
}
SPQuery newQuery = new SPQuery();
strNC_Number = "346";
int nc_Number = Convert.ToInt32(strNC_Number);
newQuery.Query = "<Where><Eq><FieldRef Name=\"NC_NUMBER\"/><Value Type=\"Text\">" + nc_Number + "</Value></Eq></Where>";
SPListItemCollection thisCollection = favList.GetItems(newQuery);
try
{
if (thisCollection.Count == 0)
{
SPListItem newItem = myWeb.Lists[listName].Items.Add();
oView.RowFilter = "NC_NUMBER = '" + nc_Number + "'";
newItem["NC_NAME"] = oView.ToTable().Rows[0]["NC_NAME"];
newItem["NC_NUMBER"] = nc_Number;
SPSite mySite = new SPSite(SPSiteGuid);
SPWeb myRootSite = mySite.OpenWeb(SPWebGuid);
//SPWeb myRootSite = SPContext.Current.Web;
String login = myRootSite.CurrentUser.LoginName;
newItem["UserLoginWithDomain"] = login;
newItem["Category"] = "Automatic";
newItem["Site Visits"] = 1;
myWeb.AllowUnsafeUpdates = true;
newItem.Update();
myWeb.AllowUnsafeUpdates = false;
}
else if (thisCollection.Count == 1)
{
SPListItem tmpItem = thisCollection[0];
tmpItem["Site Visits"] = Convert.ToInt32(tmpItem["Site Visits"]) + 1;
myWeb.AllowUnsafeUpdates = true;
tmpItem.Update();
myWeb.AllowUnsafeUpdates = false;
}
else
{
lblError.Text += "Duplicate Records exist in your my favorites list for the national client";
}
}
catch (Exception error)
{
lblError.Text += " |RecordSiteVisits: "+error.Message;
}
}
}
catch (Exception error2)
{
lblError.Text += error2.Message;
}
}
Posted on SharePoint Blogs
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks
Read the complete post at http://www.sharepointblogs.com/chadclarkesmossblog/archive/2007/10/19/problem-with-custom-web-part-big-issue.aspx