in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

This Blog

Syndication

ChadClarkesMOSSBlog

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;
            }
        }

Comments

 

RossOnMoss said:

Is there any type of error that's thrown or written to the logs?

Shoot me a line through my blog.

October 19, 2007 1:37 PM
 

Robert Sweeney said:

i had similar issues...i created a blog around it...let me know if this helps.

robertsween.blogspot.com/.../sharepoint-2007-running-elevated-code.html

October 19, 2007 3:24 PM
 

tommysegoro said:

Have you specified the "SafeControl" attribute in web.config? What about "Trust Level" in web.config? Have you set to FULL or if not using partial trust and declare your assembly in minimal_trust.config or medium_trust.config file?

If you can run the exact same code on your machine and not on your colleague's it may due to security permission. What about file permission? Have you give enough permission to the DLL?

October 20, 2007 12:36 PM
 

ChadClarke said:

I've been debugging...  It deletes the record just fine, when you refresh the record is gone, but the record remains on the page after you select delete for some unknown reason.  It works fine in my vpc, the item is gone when you click delete there....  so I can't step through the code....

October 22, 2007 9:32 AM
 

ChadClarke said:

Ok, so I got it working, well essentially I did have a couple of bugs in the code, so I cleaned those up, but all I ended up doing was to force a refresh so that the proper information displayed....  Kinda silly, but it worked.

October 22, 2007 3:09 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts