Remove Blocked File Types in SharePoint
9. January 2010 08:07

You can remove the blocked file types in SharePoint programmatically by accessing a collection on an SPWebApplication object.

Collection<string> be = webApplication.BlockedFileExtensions;
if (be.Contains("dll")) be.Remove("dll");
webApplication.Update();
Tags: Comments (0) | Permalink
Add Web Part to Page Programmatically
4. January 2010 09:32

I use the following code to programmatically add web parts to my pages.

I frequently see similar code, but no one is ever using the xml from the web part library to do the import, and as such the properties are not set to what was exported/defined in the web part xml.

Specifically:

XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());

WebPart webPart = manager.ImportWebPart(xmlReader, out errorMessage);

 

 

Enjoy:

 

public static string AddWebPartToPage( SPWeb web, string pageUrl, string webPartName, string zoneID, int zoneIndex) { using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager( pageUrl, PersonalizationScope.Shared)) { using (System.Web.UI.WebControls.WebParts.WebPart webPart =

CreateWebPart(web, webPartName, manager)) { manager.AddWebPart(webPart, zoneID, zoneIndex); return webPart.ID; } } } public static WebPart CreateWebPart(SPWeb web, string webPartName, SPLimitedWebPartManager manager) { SPQuery query = new SPQuery(); query.Query = String.Format(CultureInfo.CurrentCulture, "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>{0}</Value></Eq></Where>", webPartName); SPList webPartGallery = null; if (null == web.ParentWeb) { webPartGallery = web.GetCatalog( SPListTemplateType.WebPartCatalog); } else { webPartGallery = web.Site.RootWeb.GetCatalog( SPListTemplateType.WebPartCatalog); } SPListItemCollection webParts = webPartGallery.GetItems(query); XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream()); string errorMessage; WebPart webPart = manager.ImportWebPart(xmlReader, out errorMessage); return webPart; }

Tags: Comments (0) | Permalink
SharePoint Raw Text Field Control
4. January 2010 09:27

Sometimes you simply need to dump out the contents of a text field raw, and not have the SharePoint field controls alter you HTML, strip scripts, etc. 

I’m currently using this to display raw HTML that is being programmatically imported into a list.

This is a very simple example of how to make a field control, however you’ll want to ensure you consider things like caching and security before deploying this.

 

    public class RawTextFieldControl : BaseFieldControl
    {
        public XnaRawTextFieldControl()
        {
            base.DisableInputFieldLabel = true;
            base.ControlMode = SPControlMode.Display;
        }

        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {
            output.Write(this.ItemFieldValue);
        }
    }
Tags: Comments (0) | Permalink
SharePoint Authenticated User Panel
4. January 2010 09:22

When using SharePoint 2007 (MOSS) or SharePoint 2010 as a web content management system, it’s common to have pages show different content to logged in users and anonymous users.

Sometimes you can achieve what you want via audiences or security trimming, but often I find it’s easier to use an overloaded panel that you can include in your page layouts or master pages.  You can then set public properties to determine who the content renders for (anonymous users, specific roles, etc).

 

public class AuthenticatedPanel : Panel { public bool ShowForAnonymous { get; set; } public bool ShowForAuthenticated { get; set; } public string ShowForRole { get; set; } protected override void Render(HtmlTextWriter writer) { if (ShowForAnonymous) { base.Render(writer); return; } if (ShowForAuthenticated &&

HttpContext.Current.User.Identity.IsAuthenticated) { base.Render(writer); return; } if (!String.IsNullOrEmpty(ShowForRole) &&

HttpContext.Current.User.IsInRole(ShowForRole)) { base.Render(writer); return; } } }

Tags: Comments (0) | Permalink
Enumerating Site Templates Via PowerShell
8. December 2009 02:29

When I’m working on an extremely large SharePoint deployment, getting small code changes deployed to my dev environment to test things can be a slow process.

I’ve started using PowerShell very frequently to verify the expected return from different objects before writing the code.

Here’s how you can enumerate all the site templates installed:

[System.Reflection.Assembly]::LoadFrom

("..12 hive..\ISAPI\Microsoft.SharePoint.dll");

$globalAdmin = New-Object –TypeName 
    "Microsoft.SharePoint.Administration.SPGlobalAdmin";
$webTemplates = $globalAdmin.VirtualServers[0].GetWebTemplates("1033");
$webTemplates | Select Name, Description;




Tags: Comments (0) | Permalink