Sorting a DataTable

I have a datatable called dtSchoolResults

I grab the default view of the datatable

DataView dvSchoolResults = dtSchoolResults.DefaultView;

I specifiy the column name and sort expression for the Sort propertyof the dataview.

 dvSchoolResults.Sort = “Date Created DESC”;

I convert the view to a datatable and assign it back into dtSchoolResults

 dtSchoolResults= dvSchoolResults.ToTable();

 

The sort expression format is : COLUMN NAME SORT ORDER

e.g.  NAME ASC

First Name ASC

Last Name DESC

DOB DESC

The best overloaded method match for ‘Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(…) has some invalid arguments

I came across a issue today why attempting to use SPSecurity.RunWithElevatedPrivileges in code. The two exceptions I got when I tried building the solution was:

The best overloaded method match for ‘Microsoft.SharePoint.RunWithElevatedPrivileges(Microsoft.SharePoint.SPSecurity.CodeToRunElevated)’ has some invalid arugments

Argument ‘1’: cannot convert from ‘anonymous method’ to ‘Microsoft.SharePoint.SPSecurity.CodeToRunElevated’

And the code was

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(Web.ParentWeb.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        if (web != null)
                        {
                            String groupName = DateTime.Now() + ” Contributors”;
                           return  Groups.Get(web, groupName);

                        }                       
                    }
                }
            });
            return null;

All the two errors were trying to tell me in ‘another language was that I cannot have ‘return’ calls in the code that is being elevated.  So I moved the ‘return’ calls outside of the code that was being run under elevated privileges.

  Group group = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(Web.ParentWeb.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        if (web != null)
                        {
                            String groupName = DateTime.Now() + ” Contributors”;
                            group = Groups.Get(web, groupName);

                        }                       
                    }
                }
            });
            return group;               

 After doing this, the code compiled.

Done.

Custom SPSecurityTrimmedControl

The SPSecurityTrimmedControl is a very handy control to use especially when you want to hide a control(s) or text from the user based on their permissions on the site.

I have come across situations where I needed to extend the SPSecurityTrimmed control to limit the content displayed to the current logged in user. Permissions on the site was dynamically changing so I can’t rely on using the out of the box SPSecurityTrimmed control and setting ‘static’ permissions for it to hide/show content. Also, the item is accessible by multiple users but there were some controls and text that only the author of the item should see and use.

So…… I developed my custom SPSecurityTrimmedControl.

public partial class ShowHideUIControls:Microsoft.SharePoint.WebControls.SPSecurityTrimmedControl
 {
        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            if(IsUserTheAuthor())
            {
                base.Render(output);
            }
        }

        protected bool IsUserTheAuthor()
        {
            return SPContext.Current.Web.CurrentUser.Sid.Equals(SPContext.Current.ListItem.File.Author.Sid);
        }
 }

On the aspx page where I wanted to place this control, I added the following to the top of the page.

<%@ Register TagPrefix=”CustomTrimControl” Namespace=”CustomProjectDevelopment.UserControls” Assembly=”CustomProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=o6b1a8da1u6d1903″ %>

And I added my custom SPSecurityTrimmedControl around the content and controls I wanted to hide conditionally:

<CustomTrimControl:ShowHideUIControls runat=”server” ID=”HideUIControls1″>
          <tr>
         <td style=”font-size: 133%;  padding-top: 32px; border-bottom: 1px solid #83b0ec;”>
       Site Audit Access History      
   </td>
        </tr>
 <tr><td>
 <br /> <!– Other controls or text you want to be hidden from other users are placed here e.g. like the control below –>
    <CustomDevControl:SiteAuditAccessHistory runat=”server”  id=”customDevControl1″></CustomDevControl:SiteAuditAccessHistory>
   
 </td></tr>
 </CustomTrimControl:ShowHideUIControls>

There you have it. Everything between the <CustomTrimControl:ShowHideUIControls>… <\CustomTrimControl:ShowHideUIControls> will only be visible to the author of the item.