This example shows you how to delete a row from your SPGridView control.
Note the two events OnRowDataBound and OnRowDeleting on the control, and also pay attention to the DataKeyNames property.
I’ve also used TemplateField, and added in a LinkButton for Delete. Note the CommandName and CommandArgument properties.
Essentially, in this example I’ve bound a collection of items (SPListItem) from a particular list (SPList). The CommandArgument evaluates the Title of the list item that is also part of the bound collection.
And on the OnRowDeleting event, I’ve retrieved the list item (SPListItem) title from the particular row using the RowIndex.
Need to retrieve user information, such as email from a PickerEntity control? The code snippet below shows you how to, for this example get the user email.
Do note, I also have a post on how to do this from a People and Group field here.
Ever had to retrieve user information from a Person or Group field? In my case, I had to get the user email, and the code snippets below shows you how.
If the field has been set to only allow single selection such as
the use the following:
Where oItem is the SPListItem object, and [“Manager”] is the field display name.
However, if the field has been set to allow multi-selection such as
then use the following:
Where oItem is the SPListItem object and [“Team”] is the field display name. And sb being a StringBuilder object that I’ve used for this purpose.
This is more of a note to self kind of post, but if it helps you as well then super. The Approval Status internal field name is _ModerationStatus and it is of type ModStat, which has internal values corresponding to the status. See table below:
These values can be used to help simplify things for us, for example if I was to use CAML and retrieve all Approved documents from a particular document library the query would look something like below.
<Where><Eq><FieldRef Name=’_ModerationStatus’ /><Value Type=’ModStat’>0</Value></Eq></Where>
For those that have not heard of iTextSharp, it is a free downloadable library that is use for pdf generation, formatting and a whole lot more. I myself was introduced to it by Tommy Segoro. I must admit that it does take some effort to get into the workings of it, but the payoff is just superb.
I don’t intend to introduce you to the wonders of this library, but if you have not played with it, then I recommend paying a visit to Mikesdotnetting. He has a wonderful series of post that gets your familiarised with iTextSharp.
My snippet below just shows how to save a generated pdf into a SharePoint library directly. Hope it helps, happy days.
Document pdfDoc = new Document();
MemoryStream ms = new MemoryStream();
pdfDoc.Add(new Paragraph("This is a demo document created with iTextSharp."));
// Save document to a SharePoint document library
SPWeb oWeb = SPContext.Current.Web;
SPFolder oLibrary = oWeb.GetFolder("Demo");
string fullUrl = oWeb.Url + "/" + oLibrary.Url + "/demo.pdf";
byte data = ms.GetBuffer();
SPFile oFile = oLibrary.Files.Add(fullUrl, data);
If you’re using an .ascx Control as your web part and then load the control from a web part class then you’re probably aware that it can be hard in getting custom web part property on there. But thanks to a brilliant TechNet forum submission by Waldek Mastykarz explaining how to get around this I’ve managed to see it work myself. See below for brief explanation on it, hope it helps.
I have a custom property for Site Collection URL on my main web part class as below:
I also have a similar property on my .ascx Control, as below:
The intention is to have the property specified on my main web part class be available and used on my .ascx Control as well. And you do this on the CreateChildControls() method, as below:
string template = @”_controltemplates/HM/DemoPartControl.ascx”; specifies the location of the .ascx Control.
DemoPartControl demopart = this.Page.LoadControl(template) as DemoPartControl; casts my .ascx Control class in order to get access to it’s properties.
demoPart.TopSite = this.RootSite; sets the TopSite property on my .ascx Control to the value of the RooSite property on the main web part class.
SharePoint URL empty spaces are translated as %20. Hence, in order to ensure that are URL’s are reference correctly we would use the UrlEncode method of the SPEncode class. Be aware though, that this will return %2b instead of %20 as we would have been hoping for. In order to get around this and still have my URL’s reference correctly I use the string Contains and string Replace methods. Code snippet as below:
private string fullUrl = “<some url>”
fullUrl.Replace(“ ”, “%20”);
“is inaccessible due to its protection level at System.Web.Compilation.AssemblyBuilder.Compile()”
This was the error I got when I navigated to my custom application page (Note: I have custom errors turned off and call stack set to true on my web.config)
The problem faced here was that my class access modifier was not specified (so it defaults as private). All I had to do was change my class access modified to public and this fixed the problem.
This error came up as I attempted to build my code. No biggie here, and it was totally my own silly fault. You see, my Visual Studio project Output type was set to Console Application.
To fix this, all I did was right-clicked on my Project, then selected Properties. On the Project Properties window, I selected the Application tab. And on the Application window, I changed the Output type from Console Application to Class Library. Save the project and rebuild, no problems at all.
See code snippet below:
SPWeb oWeb = SPContext.Current.Web;
SPList oList = oWeb.Lists["Demo"];
SPListItemCollection oFolderGroup = oList.Folders;
// Create main folder (parent)
SPListItem oParentFolder = oFolderGroup.Add(“”, SPFileSystemObjectType.Folder, “foldername”)
// Create subfolder (child)
SPListItem oChildFolder = oFolderGroup.Add(“http://sharepoint/” + oParentFolder.Url, SPFileSystemObjectType.Folder, “subfoldername”);
You would probably be wondering as to what is the difference between the creation of a folder and subfolder? If you have a look at the code above, a subfolder is created with the full url of the parent folder being specified, as opposed to just “”. Please note, I said full url, this would mean you may require some string concatenation as I have done above. And please do not forget the Update() method after.