Wednesday, March 9, 2011

Keep Dropdown items attributes during async. postback

If you add custom attributes to ListItems such as color etc. the attributes are lost when a partial postback occurs. The attributes are not kept in ViewState.

Dropdown

One solution is to re-apply the styles when a partial postback occurs:

if (Page.IsPostBack)
{
    

if (ScriptManager.GetCurrent(this
.Page).IsInAsyncPostBack)
     {
        
foreach (ListItem Item in
Drp_Tasks.Items)
         {
            
if (Item.Value.Contains("WSS Task"
))
                 Item.Attributes.Add(
"Style", "color:blue"
);
            
else if (Item.Value.Contains("Timesheet Task"
))
                 Item.Attributes.Add(
"Style", "color:green");
         }
     } }


Another solution is to create a new ListItem class that keeps the attributes in ViewState.

Thursday, March 3, 2011

CAML Query and User Groups – Membership

While creating a Timesheet webpart I needed to list a users tasks in a task list. The tip from Martin Hatch gave the basics. and the query executed fine in U2U CAML Query Builder (2007 version works fine for 2010)

<Where>
  <Or>
    <Eq>
      <FieldRef Name='AssignedTo' />
      <Value Type='UserMulti'>User Display Name</Value>
    </Eq>
    <Membership Type='CurrentUserGroups'>
      <FieldRef Name='AssignedTo' />
    </Membership>
  </Or>
</Where>


This query will return users in groups, and multiple users specified in the field:


Users and Groups


Note:



The Membership attribute checks current user, so do not run the query with elevated rights (SPSecurity.RunWithElevatedPrivileges(…)



Unless you assign tasks to your application pool the query will not return items based on group assignments