SPGridView Delete Row Example
Posted: 31/08/2011 Filed under: Uncategorized | Tags: Development, SPGridView Leave a comment »This example shows you how to delete a row from your SPGridView control.
Markup
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.
Code-behind
From the code side of things, I’ve inserted a JavaScript confirm dialog that prompts the user with “Are you sure?” This is accomplished on the OnRowDataBound event.
And on the OnRowDeleting event, I’ve retrieved the list item (SPListItem) title from the particular row using the RowIndex.
Happy coding!
Retrieving user information from PickerEntity control
Posted: 24/08/2011 Filed under: Uncategorized | Tags: C#, Development, Fields, PickerEntity Leave a comment »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.
Happy coding!
Retrieving user information from Person or Group field
Posted: 24/08/2011 Filed under: Uncategorized | Tags: C#, Development, Fields 1 Comment »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.
Happy coding!
Approval Status internal values.
Posted: 20/05/2011 Filed under: Uncategorized | Tags: Configuration, Development 2 Comments »
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:
|
Status |
Internal Value |
|
Approved |
0 |
|
Rejected |
1 |
|
Pending |
2 |
|
Draft |
3 |
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>
iTextSharp and SharePoint
Posted: 12/05/2011 Filed under: Development | Tags: Development, iTextSharp 5 Comments »
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();
PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
pdfDoc.Add(new Paragraph("This is a demo document created with iTextSharp."));
pdfDoc.Close();
// 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);
oFile.Update();
Custom Web Part Property for .ascx Control
Posted: 02/05/2011 Filed under: Uncategorized | Tags: Development, Web Part Leave a comment »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:
Breakdown:
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.
SPEncode.UrlEncode returns %2b instead of %20
Posted: 10/08/2010 Filed under: Development | Tags: API, Development Leave a comment »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>”
if(fullUrl.Contains(“ ”))
{
fullUrl.Replace(“ ”, “%20”);
return fullUrl;
}
Custom Application Page “is inaccessible due to its protection level”.
Posted: 01/12/2009 Filed under: Development | Tags: Development Leave a comment »“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.
Custom Application Page "does not contain a static method suitable for an entry point"
Posted: 01/12/2009 Filed under: Configuration, Development | Tags: Configuration, Development Leave a comment »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.
Creating a Subfolder within a Folder Programmatically.
Posted: 15/10/2009 Filed under: Development | Tags: Development Leave a comment »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”)
oParentFolder.Update();
// Create subfolder (child)
SPListItem oChildFolder = oFolderGroup.Add(“http://sharepoint/” + oParentFolder.Url, SPFileSystemObjectType.Folder, “subfoldername”);
oChildFolder.Update();
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.