SPGridView Clear The Applied Fillter Programmatically SPGridView.ClearFilter()

Standard

I had a scenario like that:

I have a SPGridView with filter allowed, a DropDownList and a search button, the default behavior for the grid is to display all un-deleted users names and status which is ( Disabled or Enabled ). If I select view all users from the drop down and click search button the status is one of three (Deleted – Disabled Or Enabled) then I can filter the SPGridView to display all the deleted users.

The second step is to go the dropdown again and select un-deleted users only and click search button the result is empty grid view because the filter of SPGridView which is Status = ‘Deleted’ is sill applied.

The normal think is to go to search button handler and try to find a method like SPGridView.ClearFilter()
before you bind the grid again, but unfortunately I didn’t find a method like that.

After search on the internet I got a protected method that can do this but ‘it is protected’ the solution was like that the following steps:

1 – Create new class inherit the SPGridView class
2 – Add new method like the following to your class
public void ClearFilter()
{
this.DoFilterPostBackEventProcessing(SPGridView.PostbackFilterCommandClear);
}
3 – Register the new control to your page for example
<%@ Register Assembly=”$SharePoint.Project.AssemblyFullName$”  Namespace=”Project.NewClassNameSpace” TagPrefix=”CustomControl” %>
4 – Use the new control instead of the SPGridView
<CustomControl:NewClassName ID=”_pamsGridView” runat=”server” ……. %> …… </CustomControl:NewClassName%>
5 – Now you will find the ClearFilter method in the your Grid object
enhancedGridView.ClearFilter();

Drop Down Issue in Lookup Field

Standard

If the look-up field contains more than 20 items it convert to another structure instead of drop-down list. This happens only with IE browser

To get the drop-down back see the following article:
http://sharepointegg.blogspot.com/2010/10/fixing-sharepoint-2010-lookup-drop-down.html

The solution:

I am assuming that you have a reference to the JQuery library in your master page.

<script&gt;
$(document).ready(function () {

// Name of the column (Display Name)
var columnName = “Lookup”;

// Override the Drop Down List
OverrideDropDownList(columnName);

// Main Function
function OverrideDropDownList(columnName) {

// Construct a drop down list object
var lookupDDL = new DropDownList(columnName);

// Do this only in complex mode…
if (lookupDDL.Type == “C”) {

// Hide the text box and drop down arrow
lookupDDL.Obj.css(‘display’, ‘none’);
lookupDDL.Obj.next(“img”).css(‘display’, ‘none’);

// Construct the simple drop down field with change trigger
var tempDDLName = “tempDDLName_” + columnName;
if (lookupDDL.Obj.parent().find(“select[ID='” + tempDDLName + “‘]”).length == 0) {
lookupDDL.Obj.parent().append(“”);

lookupDDL.Obj.parent().find(“select[ID='” + tempDDLName + “‘]”).bind(“change”, function () {
updateOriginalField(columnName, tempDDLName);
});
}

// Get all the options
var splittedChoices = lookupDDL.Obj.attr(‘choices’).split(“|”);

// get selected value
var hiddenVal = $(‘input[name=’ + lookupDDL.Obj.attr(“optHid”) + ‘]’).val()
if (hiddenVal == “0”) {
hiddenVal = lookupDDL.Obj.attr(“value”)
}

// Replacing the drop down object with the simple drop down list
lookupDDL = new DropDownList(tempDDLName);

// Populate the drop down list
for (var i = 0; i < splittedChoices.length; i++) {
var optionVal = splittedChoices[i];
i++;
var optionId = splittedChoices[i];

var selected = (optionId == hiddenVal) ? " selected='selected'" : "";
lookupDDL.Obj.append("” + optionVal + “”);
}
}
}

// method to update the original and hidden field.
function updateOriginalField(child, temp) {
var childSelect = new DropDownList(child);
var tempSelect = new DropDownList(temp);

// Set the text box
childSelect.Obj.attr(“value”, tempSelect.Obj.find(“option:selected”).val());

// Get Hidden ID
var hiddenId = childSelect.Obj.attr(“optHid”);

// Update the hidden variable
$(‘input[name=’ + hiddenId + ‘]’).val(tempSelect.Obj.find(“option:selected”).val());
}

// just to construct a drop down box object. Idea token from SPServces
function DropDownList(colName) {
// Simple – when they are less than 20 items
if ((this.Obj = $(“select[Title='” + colName + “‘]”)).html() != null) {
this.Type = “S”;
// Compound – when they are more than 20 items
} else if ((this.Obj = $(“input[Title='” + colName + “‘]”)).html() != null) {
this.Type = “C”;
// Multi-select: This will find the multi-select column control on English and most other languages sites where the Title looks like ‘Column Name possible values’
} else if ((this.Obj = $(“select[ID$=’SelectCandidate’][Title^='” + colName + ” ‘]”)).html() != null) {
this.Type = “M”;
// Multi-select: This will find the multi-select column control on a Russian site (and perhaps others) where the Title looks like ‘Выбранных значений: Column Name’
} else if ((this.Obj = $(“select[ID$=’SelectCandidate’][Title$=’: ” + colName + “‘]”)).html() != null) {
this.Type = “M”;
} else
this.Type = null;
} // End of function dropdownCtl
});
</script&gt;

Add Meta Properties to SharePoint List

Standard

Sometime you want to associate your list with some other information which is a related to the list it self no to the items
One may though that using hidden field is solve this issue but it is not incredible to add a certain value to every item with the same value and what if there is no items created yet example of that you need to add information about expire date of a survey list
I have found the best solution is to add it to the RootFolder properties which is metadata about the current list
Here is an example
1- To add property at creating list
SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
Guid listID = web.Lists.Add(TextBox1.Text, “”, SPListTemplateType.Survey);
SPList list = web.Lists[listID];
SPFolder folder = list.RootFolder;
folder.Properties.Add(“Test”, web.CurrentUser.LoginName);
folder.Update();
list.Update();
web.Update();
web.AllowUnsafeUpdates = false;
}
}

});

2- To retierve this value
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
Label1.Text = web.Lists[TextBox1.Text].RootFolder.Properties[“Test”].ToString();
}
}

});

Hope this post help you all

Get SharePoint 2010 List Template ID

Standard

To get list template you will think like that
SPList.TemplateId
but this is not gonna work because there is no TemplateId in SPList object
you can get it as following
SPList.RootFolder.Properties[“vti_listservertemplate”] 🙂