Tuesday, April 17, 2012

Obtaining a List of Users from a SharePoint site via a SQL command

It's only a matter of time before your PM is going to come to you one day and ask you "so...who now has access to that site you set up about a year ago?".  Rather than having to root around the various groups and compiling the list by hand, the quick n' dirty solution to this problem is to run the following SQL command against your SharePoint content database.  This query will quickly obtain a list of users based on the SharePoint groups they belong which you can then slap it into an Excel spreadsheet for them:
SELECT DISTINCT [Groups].[Title] as 'Group', [UserInfo].[tp_Title] as 'User', [UserInfo].[tp_Email] as 'Email' FROM [Groups] INNER JOIN [GroupMembership] ON [GroupMembership].GroupId = [Groups].ID AND [GroupMembership].SiteID = [Groups].SiteID INNER JOIN [UserInfo] ON [UserInfo].tp_ID = [GroupMembership].MemberId AND [UserInfo].tp_SiteID = [GroupMembership].SiteID ORDER BY [Groups].[Title] ASC, [UserInfo].[tp_Title] ASC
By the way, please don't get the impression that I'm implying that you can write INSERT or DELETE statements against these databases.  The minute you do that is the minute that Microsoft will "disown" your SharePoint site and will not be able to provide technical support for it.  This information should be considered READ-ONLY to you and you should only add, modify, or delete users using the SharePoint interface.

Friday, April 13, 2012

Programmatically initiate javascript from code behind

Occassionally, situations may arise in which you want to initiate javascript from the C# code behind of your web application.  In the following simple example, the code in the Page Load event is triggering a simple alert message to fire; however, the sky is the limit with all the possibilities that this opens up:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (!ClientScript.IsStartupScriptRegistered(this.GetType(),"alert"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('All leases must be re-assigned to another parent before the delete operation may continue.  You may use this view to re-assign those leases.');", true);
        }
    }
}

Enjoy!

Tuesday, April 10, 2012

Launch a javascript alert or confirmation from a button located within a GridView control

I recently ran into a situation in which the business owner wanted to add a javascript popup that would confirm that the user did indeed wish to delete the selected row from a given GridView.  Originally, I was using a standard ButtonField control to display the delete button in the GridView which I've presented as follows:

<asp:ButtonField CommandName="DeleteRecord" ControlStyle-Height="23px" ControlStyle-Width="75px" ControlStyle-CssClass="GridView_Delete_Button" />

but, decided to switch gears and use the following code instead to incorporate that javascript popup:

<asp:TemplateField>
      <ItemTemplate>
           <asp:Button ID="btnDeleteParentCode" runat="server" ControlStyle-Height="23px" ControlStyle-Width="75px"
                 ControlStyle-CssClass="GridView_Delete_Button" CommandName="DeleteRecord" CommandArgument="<%# Container.DataItemIndex %>"
                 OnClientClick="return confirm('Are you certain you wish to delete the selected record');" />
     </ItemTemplate>
</asp:TemplateField>

The good news is that this new solution will work exactly like the previous ButtonField control with regards to your GridView_RowCommand event so you won't have to change any of your code in that event.  This is due to the additional CommandArgument attribute that you see in the Button control that will pass the selected row index in similar fashion to what takes place automatically with the previous ButtonField control.

In the event that you'd prefer to use a javascript alert instead of a confirmation, here's some sample code for that as well...

<asp:TemplateField>
     <ItemTemplate>
          <asp:Button ID="btnMove" runat="server" ControlStyle-Height="23px" ControlStyle-Width="75px"
               ControlStyle-CssClass="GridView_Move_Button" CommandName="Move" CommandArgument="<%# Container.DataItemIndex %>"
               OnClientClick="return alert('The record you've selected has been moved to a new location');" />
     </ItemTemplate>
</asp:TemplateField>