July 23, 2008

How to display all the web applications where a custom MembershipProvider is installed

The following piece code is a little console application which displays all the web applications where a custom MembershipProvider passed as argument is installed:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Web.Configuration;
 
namespace DisplayWebAppsUsingACustomProvider
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args == null  args.Length != 1)
                {
                    throw new ArgumentException("Invalid argument.");
                }
 
                string myMembershipProvider = args[0];
 
                SPFarm farm = SPFarm.Local;
                Console.WriteLine(string.Format("Connected to farm: {0}.", farm.DisplayName));
 
                SPWebService service = farm.Services.GetValue<SPWebService>("");
                Console.WriteLine("Services has been retrieved.");
 
                foreach (SPWebApplication webApp in service.WebApplications)
                {
                    Console.WriteLine(string.Format("Connected to Web Application: {0}.", webApp.DisplayName));
 
                    if (!webApp.IsAdministrationWebApplication)
                    {
                        foreach (SPUrlZone zone in webApp.IisSettings.Keys)
                        {
                            SPIisSettings setting = webApp.IisSettings[zone];
                            if (setting.AuthenticationMode == AuthenticationMode.Forms)
                            {
                                if (setting.MembershipProvider.ToLower() == myMembershipProvider.ToLower())
                                {
                                    Console.WriteLine(string.Format("MembershipProvider \"{0}\" found.", myMembershipProvider));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Message: {0} {1} Stack:{2}.", ex.Message, System.Environment.NewLine, ex.StackTrace));
            }
            Console.WriteLine("Press a touch to exit.");
            Console.ReadLine();
        }
    }
}

July 14, 2008

Styles not rendered when using the SharePoint DateTimeControl in custom pages

When using the SharePoint DateTimeControl in custom pages, the popup calendar is rendered without styles. I don't know why but the problem is that the datepicker.css stylesheet is not referenced as explained on the Rich Finn's blog (http://blog.richfinn.net/2006/12/10/SharePointDateTimeControlDoesNotRenderStyles.aspx).

Have a look on the iframe.aspx page located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS.

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages" %> 
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.DatePickerFrame"      %> 
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Import Namespace="Microsoft.SharePoint" %> 
<% SPSite spServer = SPControl.GetContextSite(Context); SPWeb spWeb = SPControl.GetContextWeb(Context); %>
<html dir="<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,multipages_direction_dir_value%>' EncodeMethod='HtmlEncode'/>">
    <HEAD>
    <META Name="GENERATOR" Content="Microsoft SharePoint">
    <SharePoint:CssLink runat="server"/>
    <script src="./DatePicker.js"></script>
    <title>Date Picker</title>
    </HEAD>
    <BODY onload="PositionFrame('DatePickerDiv');" onkeydown="OnKeyDown(this);" style="margin:0;">
      <SharePoint:SPDatePickerControl id="DatePickerWebCustomControl" runat="server" >
          </SharePoint:SPDatePickerControl>
    </BODY>
</HTML>

To solve the problem, you have to add manually the reference to the stylesheet.

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages" %> 
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.DatePickerFrame"      %> 
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Import Namespace="Microsoft.SharePoint" %> 
<% SPSite spServer = SPControl.GetContextSite(Context); SPWeb spWeb = SPControl.GetContextWeb(Context); %>
<html dir="<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,multipages_direction_dir_value%>' EncodeMethod='HtmlEncode'/>">
    <HEAD>
    <META Name="GENERATOR" Content="Microsoft SharePoint">
    <link rel="stylesheet" type="text/css"href="/_layouts/1033/styles/datepicker.css"/>
    <SharePoint:CssLink runat="server"/>
    <script src="./DatePicker.js"></script>
    <title>Date Picker</title>
    </HEAD>
    <BODY onload="PositionFrame('DatePickerDiv');" onkeydown="OnKeyDown(this);" style="margin:0;">
      <SharePoint:SPDatePickerControl id="DatePickerWebCustomControl" runat="server" >
          </SharePoint:SPDatePickerControl>
    </BODY>
</HTML>

July 12, 2008

How to approve a folder programmatically in SharePoint

If you want to approve a file programmatically in SharePoint, you have to use the Approve(string comment) method of the SPFile class:

file.Approve(string.Format("This file has been approved the {0}", DateTime.Now));

If you want to approve a folder, it seems obvious that an Approve method also exists in the SPFolder class. Unfortunately, it is not the case. you have to use the ModerationInformation property of the underlying SPListItem:

using (SPSite site = new SPSite("http://server:port/"))
{
    SPWeb web = site.OpenWeb();
 
    using (web)
    {
        SPList myList = web.Lists["MyList"];
        SPFolder newFolder = myList.RootFolder.SubFolders.Add("folderName");
        SPModerationInformation moderationInformation = newFolder.Item.ModerationInformation;
        moderationInformation.Comment = string.Format("This folder has been approved the {0}",DateTime.Now);
        moderationInformation.Status = SPModerationStatusType.Approved;
        newFolder.Item.Update();
    }
}

July 10, 2008

Advanced Alert for SharePoint

Advanced Alert is a Windows Sharepoint Services 3.0 solution which allows users having the ManageAlerts permission to create alerts on list or item for SharePoint groups.

In using the Alert Me feature on SharePoint lists or items, you can only create alerts for yourself or for several users. No way to create alerts for SharePoint groups. Advanced Alert adds a Alert Group link on the lists Actions menu and on the items contextual menu. It works exactly like the Alert Me links except that the Alert Group links display a page where you can create alerts for groups. Only groups having permissions to see the item/list are selectable.

You can find the solution on CodePlex: http://www.codeplex.com/AdvancedAlert