in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

This Blog

Syndication

ChadClarkesMOSSBlog

Custom Web Part Code: Deleting an Item

So this works, but if anyone has any brighter ideas, I have open ears...  I was creating a document library web part and had to recreate some of the functionality in the menu...  So here is the way I delete and Item...

Declare and initialize a hidden field to store the id of the element to be deleted...  hfDeleteDoc

//Server Side Function (C#) 
private Boolean deleteDoc(int ID){
 try
 {
  if (msaListName != null && msaListName != "")
  {
   SPContext.Current.Web.Lists[msaListName].Items.DeleteItemById(ID);
  }
  return true;
 }
 catch
 {
  return false;
 }
}

//Server Side Code (C#) 
if (hfDeleteDoc.Value != "")
{
  int id = Convert.ToInt32(hfDeleteDoc.Value);
  deleteDoc(id);
  hfDeleteDoc.Value = "";
}

//Server Side Code (C#) Building a javascript function for your menutemplate 
string deleteDocFunction = "";
deleteDocFunction += "<script>\n";
deleteDocFunction += "function deleteDoc(docID){\n";
deleteDocFunction += "   var hfDeleteDoc = document.getElementById('" + hfDeleteDoc.ClientID + "');\n";
deleteDocFunction += "   hfDeleteDoc.value = docID;\n";
deleteDocFunction += "   document.forms[0].submit();\n";
deleteDocFunction += "}\n";
deleteDocFunction += "</script>\n";
writer.Write(deleteDocFunction);

//Server Side Code (C#) Creating a template for your menu
msaListMenu = new MenuTemplate();
msaListMenu.ID = "MSAListMenu";
MenuItemTemplate msaListMenuItem1 = new MenuItemTemplate("View Properties");
msaListMenuItem1.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/MSA/Forms/DispForm.aspx?ID=%EDIT%";
msaListMenu.Controls.Add(msaListMenuItem1);
SPSite tmp = SPContext.Current.Site;
//SPGroup
//SPGroup tmpGrp = SPContext.Current.Web.SiteGroups["Content Managers"];
//tmpGrp.ContainsCurrentUser

MenuItemTemplate msaListMenuItem3 = new MenuItemTemplate("Edit Properties", "/_layouts/images/EDIT.GIF");
msaListMenuItem3.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/MSA/Forms/EditForm.aspx?ID=%EDIT%";
msaListMenu.Controls.Add(msaListMenuItem3);

MenuItemTemplate msaListMenuItem4 = new MenuItemTemplate("Delete", "/_layouts/images/DELETE.GIF");
msaListMenuItem4.ClientOnClickScript = "deleteDoc(%EDIT%);";
//msaListMenuItem4.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/MSA/Forms/EditForm.aspx?ID=%EDIT%";
msaListMenu.Controls.Add(msaListMenuItem4);

MenuItemTemplate msaListMenuItem2 = new MenuItemTemplate("Alert Me");
msaListMenuItem2.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/_layouts/SubNew.aspx?List={49DFCC0E-E021-41F7-9978-1557E1A4B64A}&ID=%EDIT%";
msaListMenu.Controls.Add(msaListMenuItem2);

Comments

 

Tim Dobrinski said:

In what context are you deleting an item?  Are you building a webpart to delete an item?  I am curious to your methods, but I'd like to know the scenario you're running this in to need to delete an item.

Thanks

September 26, 2007 3:54 PM
 

ChadClarke said:

Sorry, didn't give enough background I suppose.  I'm pulling list items into an SPGridView Control and setting up an SPMenuField with a MenuItemTemplate.  The web part has search capability through caml queries...

September 27, 2007 8:16 AM
 

DV said:

In the ID value u have given it as %EDIT%. what does that mean. And how is it possible to get to know the ID value of a document .

Thanks

December 10, 2007 5:50 AM
 

ChadClarke said:

I apologize for the lack of code...  In this sample I only declare and initialize the menu template.  Refer to Powlo's blog for more information on the SPMenuField.  blogs.msdn.com/.../displaying-custom-data-through-sharepoint-lists-using-spgridview-and-spmenufield.aspx

On the SPMenuField there is a property where you set up the EDIT variable.

IE.

colMenu.TokenNameAndValueFields = "EDIT=ID,NAME=PresenterName";

December 12, 2007 10:25 AM
 

Chander said:

Hi where do you create the Hidden Field,in the CreateChildControls method?

Does the javascript work for you?For example If I had to assign the returning value of the javascript function into the hidden field?

Thanks

December 19, 2007 7:25 PM
 

ChadClarke said:

Yes, the javascript worked.  yes you can assign the returning value to a hidden field.  the yes, declare the hidden field above the create child controls, then instantiate it in the createchildcontrols method.  add the control the the this.Controls collection so that it can maintain session state.

Good luck in the world of sharepoint!

December 28, 2007 5:11 PM
 

Steve said:

MenuItemTemplate msaListMenuItem2 = new MenuItemTemplate("Alert Me");

msaListMenuItem2.ClientOnClickNavigateUrl = SPContext.Current.Web.Url + "/_layouts/SubNew.aspx?List={49DFCC0E-E021-41F7-9978-1557E1A4B64A}&ID=%EDIT%";

I'm interested to know if this actually results in alert emails being generated.  We have seen that although the alert gets created something key is missing and never results in alert emails being recieved by the user requesting the alert.  In Fact Microsoft has said the call to the page SubNew.aspx is implemented using a post and can't be recreated by simply calling the url and passing parameters.  Microsoft has directed me to Nishand's Blog at blogs.msdn.com/.../how-to-create-searchalert-programmatically.aspx but this creates the alert at the site context level and not the subsite where the alert is created.  How did yours turn out?

February 12, 2008 7:47 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