2/08/2010

SharePoint: Prevent users from adding “NT AUTHORITY\authenticated users” and other selected accounts to sites and groups.

 

Both 2007 and 2010 examples are here. The 2010 version is half way down the page....

 

Who is "NT AUTHORITY\Authenticated Users"?

The user "NT AUTHORITY\Authenticated Users" represents every account that can logon to your network. In the typical environment that would include employees, contractors, vendors with a "special account", anyone with Windows Authenticated access to the network.

 

SharePoint makes it too easy to add “NT AUTHORITY\authenticated users” to a site:

image

 

 

How to block accounts (SharePoint 2007 WSS and MOSS)

The following requires an edit to a LAYOUTS Application page. Best Practice or your governance policies may not permit this.

That said…

  1. Navigate to the 12 hive to ..\12\TEMPLATES\LAYOUTS
  2. Right-click copy / right-click paste  (to back it up, just in case)
  3. Open aclinv.aspx with Notepad or your favorite editor (one that will not mess the HTML in the page)
     
  4. Search and find “LinkAddAuthUsers” and comment out the ASP:LinkButton
     
  5. <!--
    <asp:LinkButton id="LinkAddAuthUsers" 
    Text="<%$Resources:wss,permsetup_addauthtitle%>" runat="server"
    CausesValidation="false" OnClick="LinkAddAuthUsers_Click" />
    —>
      
     
  6. Now to make sure they cannot still type it in (or any other account you want to block) add a JavaScript function to check for forbidden accounts and cancel the postback. Edit the IF statement to add any other accounts you want to block. This example blocks “NT AUTHORITY\authenticated users” and “Domain\domain users”.

    Add the following JavaScript at the end of the page just before the last line (</asp:Content>).

  7.  
    <script>
     // techtrainingnotes.blogspot.com/2010/02/sharepoint-prevent-users-from-adding-nt.html
     
    var clkfun;
     
    _spBodyOnLoadFunctionNames.push('HookUpCheckUsers');
     
     
    function HookUpCheckUsers()
    {
      var buttonname='ctl00$PlaceHolderMain$ctl02$RptControls$btnOK';
      // get the current onclick function
      clkfun = document.getElementById(buttonname).onclick;
      // and replace it with our function
      document.getElementById(buttonname).onclick=CheckUsers;
    }
     
    function CheckUsers()
    {
      var divname='ctl00_PlaceHolderMain_ctl00_ctl01_userPicker_upLevelDiv'
      if ( document.getElementById(divname).innerHTML.toLowerCase().indexOf('nt authority\\authenticated users') > -1  
        || document.getElementById(divname).innerHTML.toLowerCase().indexOf('domain\\domain users') > -1 )
      {
        alert("'NT AUTHORITY\\authenticated users' and 'Domain\\domain users' are not permitted");
        return false;  //cancel the postback
      }
      else
      { // call their function
        clkfun()
      }
    }
    </script>
     

Save your changes, go and try to add these accounts to a site.

 

Copy this file to each web front end server.
 

Test...
 

Add to your disaster recovery plan documentation!

 

 

How to block accounts (SharePoint 2010 November Beta 2)

The following requires an edit to a LAYOUTS Application page. Best Practice or your governance policies may not permit this.

That said…

  1. Navigate to the 14 hive to ..\14\TEMPLATES\LAYOUTS
  2. Right-click copy / right-click paste  (to back it up, just in case)
  3. Open aclinv.aspx with Notepad or your favorite editor (one that will not mess the HTML in the page)
     
  4. Add a JavaScript function to check for forbidden accounts and cancel the postback. Edit the IF statement to add any other accounts you want to block. This example blocks “NT AUTHORITY\authenticated users” and “Domain\domain users”.

    Add the following JavaScript at the end of the “PlaceHolderMain” content block, just before the </asp:Content> tag.
    (This is line 254 in my copy and the only </asp:Content> with a </table> just above it.)

    This code is identical to the 2007 version except for the ID of the button.


  5.  
    <script>
     // techtrainingnotes.blogspot.com/2010/02/sharepoint-prevent-users-from-adding-nt.html
     
    var clkfun;
     
    _spBodyOnLoadFunctionNames.push('HookUpCheckUsers');
     
     
    function HookUpCheckUsers()
    {
      var buttonname='ctl00_PlaceHolderMain_ctl01_RptControls_btnOK';
      // get the current onclick function
      clkfun = document.getElementById(buttonname).onclick;
      // and replace it with our function
      document.getElementById(buttonname).onclick=CheckUsers;
    }
     
    function CheckUsers()
    {
      var divname='ctl00_PlaceHolderMain_ctl00_ctl01_userPicker_upLevelDiv';
      if ( document.getElementById(divname).innerHTML.toLowerCase().indexOf('nt authority\\authenticated users') > -1  
        || document.getElementById(divname).innerHTML.toLowerCase().indexOf('domain\\domain users') > -1 )
      {
        alert("'NT AUTHORITY\\authenticated users' and 'Domain\\domain users' are not permitted");
        return false;  //cancel the postback
      }
      else
      { // call their function
        clkfun()
      }
    }
    </script>

 

Save your changes, go and try to add these accounts to a site.

 

Copy this file to each web front end server.
 

Test...
 

Add to your disaster recovery plan documentation!

 

.

2/07/2010

SharePoint: Prevent Accidental Overwrites when Uploading to a Library

This applies to both SharePoint 2007 and SharePoint 2010. The only difference is the page name, upload.aspx for SP 2007 and uploadex.aspx for SP 2010.

 

Problem… Users are uploading files and are accidentally overwriting existing files, or if versioning is enabled, accidentally adding a new version to the wrong file (one with the same name as the upload).

 

One possible solution would be to modify the upload.aspx page (uploadex.aspx in 2010) to change the default of the "Overwrite existing files" checkbox. This normally defaults to checked. (I tell people that "Overwrite existing files" is short for "Overwrite existing files and don't tell me that I'm about to destroy days of someone else's work")

        Click for full size

 

You will need access to the web servers…
The edit is simple, but you will be modifying a LAYOUTS page on the web server. This generally to avoided as it may get overwritten on the next service pack update, and the change must be part of your disaster recovery plan and these files are not backed up with the content databases.


That said... do the following at your own risk...

  1. On the web server (and this must be duplicated on each web server) drill down to the SharePoint 12 hive and find "12\TEMPLATE\LAYOUTS\upload.aspx".  (NOTE: for SharePoint 2010 edit uploadex.aspx)
     
  2. Play it safe and Right-click, Copy and Right-click Paste to make a backup!
  3. Open the file with Notepad. Search for "Overwritesingle" and just after that change "true" to "false"
        <asp:CheckBox id="OverwriteSingle" Checked="false" ...
     
  4. Search for "Overwritemultiple" and just after that change "true" to "false"
        <asp:CheckBox id="OverwriteMultiple" Checked="false" ...
  5. Save the file and go upload a file and see if it does what you want

This takes care of Upload and Upload Multiple.


Windows Explorer will prompt the user by default "This folder already contains a file named....", so that’s covered.

This edit will not impact email enabled libraries and overwrites or new versions are still possible.


One final note... the same checkbox is used for versioning and says "Add as a new version to existing files" instead of "Overwrite existing files". After the above edits if the user forgets to checkmark the box, they just get a "file exists" error.

 

 

.

2/03/2010

SharePoint 2010: What’s the same for Site Owners…

The following is for the November 09 Beta 2…

Most of the articles I see about SharePoint 2010 are about what’s new. To add some comfort for people who are considering upgrading from 2007 to 2010 I thought I would start to document what has not changed, or has only changed a little. So here is a list of things, features and screens that have not changed to much.

This is work in progress… please check back…

This list is for the Site Owners… (Dev list coming soon)

 

  • Site Actions, Site Settings
    • Although it looks completely different, very little has changed. Click the image below to see how the “old” maps to the “new”, and what is really new (circled in green).  Almost looks like they moved things around just to move things around! 
      Site Settings for a Team Site (i.e. non-publishing)  (Click for a bigger image) 
      image
                                       what a spider web!  but it shows that it’s still all there…
    • So what is new?
      • All People (just a new link, not a new item. Same as in 2007 by clicking People and Groups and then clicking All People.)
      • Themes – a new gallery of Themes (Themes can be uploaded, or downloaded. The files are ZIP files! Download one, rename it to .ZIP and see what is inside!)
      • Solutions – These are new “Sandboxed” solutions where can deploy custom code directly to a site collection instead physically to the server drives.
      • FAST – Microsoft’s other search (extra cost) technology.  If you do not have FAST installed you will just get: “Error: Access to this functionality requires FAST Search for SharePoint be installed.”
      • Content Type Publishing - (Lots of blog articles! HERE)
      • SharePoint Designer Settings – Let you limit site user access to features of… guess what…)
      • Visual Upgrade – SharePoint 2007 sites can be left with the 2007 look and feel and later be upgraded to the 2010 look and feel (and the Ribbon).
      • Help settings – Lets you exclude from Help features you may not have installed, like the Office Web Apps.
  • Site Actions, Create (2007) or More Options (2010)
    • There is a new user interface, but the old interface is still available at /_layouts/create.aspx. The new interface is a Silverlight widget that pops up over the Settings.aspx page. The old style interface will be displayed if the user’s browser does not have Silverlight installed. I still prefer the old style interface! Big icons do not make for a better experience.
       
    • For a table of lists and site templates see here
       
    • New list types?
      • Asset Library
      • External List
      • Status List
      • The “new” items in the Pages and Sites section are not new. They include a direct link to the Blog template and Content Page (the old Basic Page, but you have not choice of libraries, it is now always saved in SitePages). In Beta 2 both “Team Sites” and “Sites and Workspaces” both link to the old “Sites and Workspaces” page (but with slightly different URLs!).
         
    • The new user interface: (click for a bigger image) 
      image

    • The 2007 vs. 2010 Create pages (using the 2010 old style interface)
      New items circled.
      image_thumb[10]

    more to come as I get time…

  • 2/02/2010

    Silly Word 2007 Tricks - Lorem ipsum!

     

    Open a new document in Word 2007,

     

    type     =lorem(3,8)     and press enter...    Lorem ipsum!

     

    move to a blank line and type    =rand(10,5)    and press enter...     you are now a tech writer!

     

    again...     =rand.old(5,5)  and press enter…    and you are a typewriter tester!

     

     

    Now get back to work............

     

     

    and when you have some free time… click here…  http://en.wikipedia.org/wiki/Lorem_ipsum

    2/01/2010

    Cincinnati SharePoint User Group Meeting (February 2010)

     

    We have a new URL! http://www.CincinnatiSPUG.org
    (this still works:  http://cincyspug.securespsites.com )

     

    We have door prizes!  In addition to free pizza, we have a number of SharePoint books, mice, Windows 7 Ultimate, X-Box games and other goodies to give away!
    We will also have some discount codes from several major book publishers just for people who attend.

     

     

    February 2010 Meeting agenda

    Date: Feb. 4, 2010, Thursday

    Time: 6:00 – 8:15 pm.

    Where: Max Technical Training, 4900 Parkway Dr. #160 Mason, OH.

    Click on the link for directions http://www.maxtrain.com/directions/

    Agenda:

    • 6:00 – 6:30 - Socials and Networking - No Registration required

    • 6:30 – 6:35 - Introduction of Agenda and Speakers

    • 6:35 - 8:15 - SharePoint 2007 Best practices of Web part development
                   – Matt Tallman of RCM

    We will take a look at some known best practices for developing and deploying web parts within SharePoint environment. You will get armed with some valuable information that you may want to consider for your next web part development project. Learn how to develop web parts efficiently and how to separate UI logic from code, learn how to encapsulate web parts into features and deploy them easily to your SharePoint environment. 

     

    .

    SharePoint 2010: VHDs Now Available

     

    Update: Microsoft has released the RTM VHDs for 2010. See here: http://techtrainingnotes.blogspot.com/2010/05/sharepoint-2010-evaluation-vhd-download.html

     

     

     

     

    (2007 VPC/VHD info here: http://techtrainingnotes.blogspot.com/2008/01/sharepoint-vpcs.html)

     

    Updates:

     

    I have updated this article with more info about the Microsoft VHD download…

    • I have added some more info on the install process
    • I have added a list of what I “found” in the VHDs (password, software installed, etc)
    • I have documented some version and setup info (IPs etc)

     

    Download VHDs from Microsoft (below) or build your own…

    Step by step build using  VMWARE Workstation
    http://faizal-comeacross.blogspot.com/2009/11/step-by-step-sharepoint-server-2010.html

    Step by step build using  VirtualBox (http://www.virtualbox.org/ )
    http://www.ericharlan.com/Moss_SharePoint_2007_Blog/how-to-install-sharepoint-2010-guide-a166.html

    A good comparison of what others have done:
    http://www.sharepointjoel.com/Lists/Posts/Post.aspx?List=0cd1a63d%2D183c%2D4fc2%2D8320%2Dba5369008acb&ID=298


     

    SharePoint 2010 Beta VHDs now available

    Microsoft has released a two VHD set of images to help you evaluate just about everything in SharePoint 2010, including FAST search, Project Server 2010, Visual Studio 2010 and Office 2010.  “Everything” comes with a price… 13 GB of download!  And… these expand into 47 GB VHDs! 

    You will need Windows Server 2008 R2, 50 GB of disk, 8 GB of RAM and Hyper-V to run these.

    The description and instructions on the download page are formatted as two big paragraph with everything run together, so I have reformatted it here so you can see the cool stuff preinstalled for you.

     

    Download here:


    http://www.microsoft.com/downloads/details.aspx?FamilyID=0c51819b-3d40-435c-a103-a5481fe0a0d2&displaylang=en

     

     

    (text from the link above… see the link for the latest info)

    Overview

    This download contains a two Virtual Machine set for evaluating and demonstrating Office 2010 and SharePoint 2010.

     

    Virtual machine “a” contains the following pre-configured software:

    1. Windows Server 2008 SP2 Standard Edition x64, running as an Active Directory Domain Controller for the “CONTOSO.COM” domain with DNS and WINS
    2. Microsoft SQL Server 2008 SP2 Enterprise Edition with Analysis, Notification, and Reporting Services
    3. Microsoft Office Communication Server 2007 R2
    4. Visual Studio 2010 Beta 2 Ultimate Edition
    5. Microsoft SharePoint Server 2010 Enterprise Edition Beta 2
    6. Microsoft Office Web Applications Beta 2
    7. FAST Search for SharePoint 2010 Beta 2
    8. Microsoft Project Server 2010 Beta 2
    9. Microsoft Office 2010 Beta 2 10. Microsoft Office Communicator 2007 R2

     

    Virtual machine “b” contains the following pre-configured software:

    1. Windows Server 2008 R2 Standard Evaluation Edition x64, joined to the “CONTOSO.COM” domain
    2. Microsoft Exchange Server 2010 Active directory has been preconfigured over 200 “demo” users with metadata in an organizational structure. All of these user profiles have been imported and indexed for search within SharePoint Server 2010, with “contoso\administrator” granted administrator permissions.

    SharePoint Server 2010 has been configured in a “Complete” farm using Kerberos authentication and the default SQL Server 2008 instance for data, and has a site collection created using the Team Site template at http://intranet.contoso.com/ and a FAST Search Center at http://intranet.contoso.com/search/.

     

    Performance Considerations

    1. If possible, unpack and run the VM image on a separate, fast hard drive (7200 RPM or better) from the operating system of the host machine. If this is being done on a laptop, a second internal drive or external eSATA drive works best, though USB 2.0 (make sure it's 2.0, 1.1 is too slow) or Firewire is acceptable. For absolute best performance use a second internal SSD drive.

     

    Install

     

    Note: The instructions on the download page are not quite complete and I have learned a few things with multiple installs…  I have added comments in red…

    Note: If you choose to create a new virtual machine instead of importing the exact configuration then be careful about what the “virtual hardware” looks like or you will have to reauthenticate Windows (and you don’t know the key!”.  In a nutshell:  both machines: a virtual LAN named “internal”, two virtual CPUs and 5 GB for the “a” machine and 1 GB for the “b” machine.

    Instructions:

    You will need: Windows Server 2008 R2 with Hyper-V and at least 8 GB of RAM

    Extract the files by double-clicking the EXEs from the download. (I created a folder name “C:\SP2010Demo\” and under the files creating  “C:\SP2010Demo\Ext 2010-4a” and “C:\SP2010Demo\Ext 2010-4b”

    1. Start Hyper-V Manager from Control Panel -> Administrative Tools
    2. Confirm that the local host machine appears in the Hyper-V Manager list and select it if not already done
    3. Under Actions, click Virtual Network Manager…
    4. Confirm that you have created an Internal virtual network named “Internal”. (Only use the name “Internal”! Any other name and the import “will succeed with errors” and you will need to reconnect the virtual network and reconfigure the IP4 settings – see notes below for correct IP addresses to use) Internal networks limit connectivity to only VMs and the host. If a suitable not, create one now using the following steps:
      1. Click on Virtual Network Manager in the Actions pane
      2. Choose New virtual network in the Virtual Networks pane
      3. Choose Internal from the type list and click Add
      4. Enter a name of Internal and click OK
      5. On the local server, not a virtual machines, click Start menu -> right-click Network –> Properties
      6. Click Change adapter settings
      7. Find the adapter with a description of Internal, right-click and choose Properties
      8. Double-click on Internet Protocol Version 4 and enter the following values:
        1. IP address: 192.168.150.6
        2. Subnet mask: 255.255.255.0
        3. Default gateway: (leave blank)
        4. Preferred DNS server: 192.168.150.1
      9. Click OK
    5. Close the Virtual Network Manager dialog.
      1. Under Actions, click Import Virtual Machine…
      2. Use the Browse button to select the folder where the virtual machine package was extracted. Do not check “Duplicate all files”
        They did not mention to select “Copy” or “Move”. I used “Move” and had no problems".
      3. Click Import and wait for the Import to complete – the import status will appear in the Operations column
      4. Select the newly imported virtual machine and click Settings in the right pane of the Hyper-V Manager
      5. Confirm (and correct if necessary) that the Network Adapter is connected to the Internal network from step 1d.
      6. Also review the memory settings and adjust if needed for your machine. Don’t change the number of virtual CPUs as this invalidated the “authorized install”…
    6. Close the virtual machine Settings dialog.
    7. Start the Ext 2010-4a machine first as it is the domain controller (and just about everything else except for Exchange)
    8. After a short while you will be asked to restart the virtual machines “to apply changes”.

     

    What I found in the VHDs…

    • The password is pass@word1
    • The screen resolution is set at 1024 x 768
    • RAM is set to 5 GB for the “a” machine and 1 GB for the “b” machine
       
       
    • VHDs:

    VHD: Ext 2010-4a

    Computer name: demo2010a
    Full computer name: demo2010a.contoso.com
    Computer description: <blank>
    Domain: contoso.com

    Windows Server 2008 R2 Standard

    Password:  pass@word1

    Primary roles:
      SharePoint Server   14.0.4514.1009 (and some 14.0.4536.1000)
      FAST server
      Active Directory
      DNS

    Required IP address: 192.168.150.1

    VHD: Ext 2010-4b

    Computer name: demo2010b
    Full computer name: demo2010b.contoso.com
    Computer description: Exchange Server 2010
    Domain: contoso.com

    Windows Server 2008 R2 Standard

    Password:  pass@word1

    Primary roles:
      Exchange Server

    Required IP address: 192.168.150.2

     

    • Software installed:

    VHD: Ext 2010-4a

    Microsoft Office Communicator (and Office Communicator Server)
     
    Microsoft FAST Search Server for SharePoint 2010
     
    Microsoft Office
      Microsoft Access 2010 (Beta)
      Microsoft Excel 2010 (Beta)
      Microsoft InfoPath Designer 2010 (Beta)
      Microsoft InfoPath Filler 2010 (Beta)
      Microsoft Office 2010 Tools
      Microsoft OneNote 2010 (Beta)
      Microsoft Outlook 2010 (Beta)
      Microsoft PowerPoint 2010 (Beta)
      Microsoft Project 2010 (Beta)
      Microsoft Publisher 2010 (Beta)
      Microsoft SharePoint Designer 2010 (Beta)
      Microsoft SharePoint Workspace 2010 (Beta)
      Microsoft Visio 2010 (Beta)
      Microsoft Word 2010 (Beta)
     
    Microsoft Office 2010 Developer Resources
      Microsoft Visio 2010 SDK
     
    Microsoft SharePoint 2010 Products
      SharePoint 2010 Central Administration
      SharePoint 2010 Management Shell
      SharePoint 2010 Products Configuration Wizard
     
    Microsoft Silverlight 3 SDK
     
    Microsoft SQL Server 2005
      Microsoft Visio 2010 SDK
     
    Microsoft SQL Server 2008
      Analysis Services
      Configuration Tools
      Documentation and Tutorials
      Import and Export Data (32-bit)
      Import and Export Data (64-bit)
      Integration Services
      Performance Tools
      SQL Server Business Intelligence Development Studio
      SQL Server Management Studio
     
    Microsoft Sync Framework
      Microsoft Sync Framework Documentation
     
    Microsoft Visual Studio 2008
      Microsoft Visual Studio 2008 Documentation
      Microsoft Visual Studio 2008
      Visual Studio Tools
     
    Microsoft Visual Studio 2010
      Microsoft Test and Lab Manager
      Microsoft Visual Studio 2010 - ENU
      Microsoft Visual Studio 2010 Documentation
      Microsoft Windows SDK Tools
      Team Foundation Server Tools
      Visual Studio Tools
     
    SharePoint 2010 All Up Demo
     
    Windows PowerShell 1.0
      (empty)
     
    Windows PowerShell V2 (CTP3)
      Getting Started
      Quick Reference
      Release Notes
      User Guide
      Windows PowerShell ISE (CTP3)
      Windows PowerShell ISE (X86)(CTP3)
      Windows PowerShell V2 (CTP3)
      Windows PowerShell V2 (X86)(CTP3)

    1/31/2010

    SharePoint: Using JavaScript in a Feature CustomAction

     

    You can add to most of SharePoint’s menus by creating a Feature to define a CustomAction. When using CustomAction you need to supply a <UrlAction> element with the URL of the page to redirect to. When you replace the URL with JavaScript some weird things happen:

     

    If you have:
        <UrlAction Url="javascript:alert('hello')"/>
    SharePoint renders it as:
        onMenuClick="window.location = 'javascript:alert(\'hello\')';"
    This works just fine as Alert returns null.

    If you have JavaScript that sets a value (=) then the JavaScript returns the result and the <A> tries to redirect to that result:  (This example sets the URL to an <IFRAME> to a page in LAYOUTS)
        <UrlAction Url="javascript:document.getElementById('getthetime').src
                                           = 'http://yoursite/_layouts/CurrentTime.aspx'"/>
    SharePoint again renders it as "window.location=" and clicking the link redirects to a blank page with the SRC value displayed.

     

    The trick it to make sure your JavaScript code returns NULL, so wrap your code in VOID()
          <UrlAction Url="javascript:void(document.getElementById('getthetime').src
                                           = 'http://yoursite/_layouts/CurrentTime.aspx')"/>
    The JavaScript now runs and there is no page redirect.

     

    In general:

           <UrlAction Url="javascript:void(your javascript code)"/>

     

     

    Here’s an example feature:

    Assuming that you have a page in LAYOUTS named CurrentTime.aspx you want to load into an <IFRAME> with an ID of “getthetime)…

    feature.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <Feature  Id="9fa44e31-b703-4094-ad7c-2f3d9eac6f64"
              Title="MenuWithJS"
              Description="Demo of using JavaScript in a Custom Action"
              Version="1.0.0.0"
              Hidden="FALSE"
              Scope="Web"
              xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="elements.xml"/>    
      </ElementManifests>
    </Feature>

     

    elements.xml file: (this example adds the new menu choice to the Site Actions menu)

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <!-- Add the action to the Site Actions Menu Dropdown -->
      <CustomAction Id="JSdemo"
        GroupId="SiteActions"
        Location="Microsoft.SharePoint.StandardMenu"
        Sequence="1000"
        Title="JavaScript Test">
        <UrlAction Url="javascript:void(document.getElementById('getthetime').src = 'http://maxsp2007/sites/training/_layouts/CurrentTime.aspx')"/>
      </CustomAction>
    </Elements>

     

     

    Note: only tested in Internet Explorer…

    1/30/2010

    SharePoint: SafeControl Generator

     

    Is your new web part not showing up in the SharePoint Web Part Gallery, New Web Parts page (NewDwp.aspx)?  It seems the number one problem for new (and experienced) SharePoint web part developers is getting the <SafeControl> element of the web.config file right. (especially for VB developers… see here). So here is a little console application that will read the namespace and class information from your assembly and give back a properly formatted <SafeControl> element.

     

    Here is a typical SafeControl entry:

       1: <SafeControl 
       2:   Assembly="VBWebPart, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e7bc14c6318d16e3" 
       3:   Namespace="MaxTrain.Demo" 
       4:   TypeName="VBWebPart" 
       5:   Safe="True" />

     

    Notes:

    2: Assembly: If the assembly is signed, then this is the four part name. This can be determined with the code below, or if you have deployed the assembly to the GAC, from the data displayed in the GAC (and a dozen other tools)

    3: Namespace: For C# the namespace you supplied in your code:     namespace MyNamespace {}
                                 For VB the name space in Project Properties plus the namespace you supplied in code

    4: TypeName: Either the name of your class, or “*” for all classes in your assembly

    5: Safe:  always True if you want the web part to work!

     

    To create your own SafeControl element creator:

    Create a new C# console application and name it something like SafeControlGenerator (no additional references needed). Copy and paste the following, build and test!

     

    Usage:

       C:\>  SafeControlGenerator.exe pathtoyourassembly

    I.e.

       C:\>  SafeControlGenerator.exe C:\Webparts\ClassLibrary1\bin\Debug\MyWebPart.dll

     

    using System;
    using System.Reflection;
     
    namespace SafeControlGenerator
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length != 1)
                {
                    Console.WriteLine("Format:  SafeControlGenerator  pathToDll");
                    return;
                }
     
                string assemblyPath = args[0];
                Assembly a = Assembly.LoadFrom(assemblyPath);
     
                string fullname = a.FullName;
                if (fullname.Contains("PublicKeyToken=null")) 
                    fullname = a.GetName().Name;
                
                //Console.WriteLine("Fullname:");
                //Console.WriteLine(fullname);
                //Console.WriteLine();
     
                foreach (Type t in a.GetExportedTypes())
                {
                    //Console.WriteLine(t.Namespace + " " + t.Name);
                    Console.WriteLine("<SafeControl Assembly=\"{0}\" Namespace=\"{1}\" TypeName=\"{2}\" Safe=\"True\" />",
                                      fullname, t.Namespace, t.Name);
                    Console.WriteLine();
                }
     
            }
        }
    }

     

    .

    Note to spammers!

    Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.