none
SiteDefinitions installed within the same powershell "session" not available upon site collection creation

    Question

  • We have an issue with creating a new site collection based on a SiteDefinition located in a solution which has just been installed.

    Our powershell script look like this (pseudo code):

    New-SPWebApplication
    Add-SPSolution
    Install-SPSolution
    Install-SPFeature
    Enable-SPFeature
    iisreset
    New-SPSite

    The New-SPSite command returns the following warning : "Warning: Template is not found and is not applied"

    If we run Get-SPWebTemplate in the same powershell "session" out newly deployed SiteDefinition does not show up, however if we open a new "session" our new SiteDefinition is available.

    Somehow it seems that various SharePoint objects are cached or some kind of state is preserved throughout the session.

    Is there a way to reset this cache/state?

    Friday, April 16, 2010 10:41 AM

Answers

  • We succeeded in forcing the sharepoint cache to reload using the following commandlet, i hope this helps someone :) 
      /// <summary>
      /// Orignal 2007 code taken from: http://www.codefornuts.com/2009/10/forcing-sharepoint-tool-to-reload-12.html
      /// This cmdlet needs to be run after adding sitetemplates and before using 
      /// them. To be on the safe side always use an IISREST as well 
      /// (just before calling this).
      /// Other resource: http://wss.made4the.net/archive/2009/03/31/site-definition-issues-file-or-arguments-not-valid-for-site-template-customportaltemplate0.aspx
      /// </summary>
      [Cmdlet(VerbsCommon.Reset, "SPContext")]
      public class RecycleSPContext : PSCmdlet
      {
        [DllImport("kernel32.dll")]
        private static extern IntPtr FreeLibrary(IntPtr library);
    
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    
        [DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibrary(string lpFileName);
    
    
        protected override void ProcessRecord()
        {
          Type sprequestmanager = typeof(SPFarm).Assembly.GetType("Microsoft.SharePoint.SPRequestManager", true, true);
          Type spthreadcontext = sprequestmanager.Assembly.GetType("Microsoft.SharePoint.Utilities.SPThreadContext");
          MethodInfo setcontext = spthreadcontext.GetMethod("Set", BindingFlags.Static | BindingFlags.NonPublic);
          Type[] genericArguments = new Type[] { sprequestmanager };
          MethodInfo setcontextgeneric = setcontext.MakeGenericMethod(genericArguments);
          // set the current sprequest manager to null!
          setcontextgeneric.Invoke(null, new object[] { null });
    
          IntPtr p = GetModuleHandle("OWSSVR.DLL");
          FreeLibrary(p);
          string stsadmPath = SPUtility.GetGenericSetupPath("ISAPI");
          p = LoadLibrary(stsadmPath + @"\OWSSVR.DLL");
    
          WriteObject("SharePoint context restarted");
        }
      }
    
    Monday, April 26, 2010 7:40 AM

All replies

  • We succeeded in forcing the sharepoint cache to reload using the following commandlet, i hope this helps someone :) 
      /// <summary>
      /// Orignal 2007 code taken from: http://www.codefornuts.com/2009/10/forcing-sharepoint-tool-to-reload-12.html
      /// This cmdlet needs to be run after adding sitetemplates and before using 
      /// them. To be on the safe side always use an IISREST as well 
      /// (just before calling this).
      /// Other resource: http://wss.made4the.net/archive/2009/03/31/site-definition-issues-file-or-arguments-not-valid-for-site-template-customportaltemplate0.aspx
      /// </summary>
      [Cmdlet(VerbsCommon.Reset, "SPContext")]
      public class RecycleSPContext : PSCmdlet
      {
        [DllImport("kernel32.dll")]
        private static extern IntPtr FreeLibrary(IntPtr library);
    
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    
        [DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibrary(string lpFileName);
    
    
        protected override void ProcessRecord()
        {
          Type sprequestmanager = typeof(SPFarm).Assembly.GetType("Microsoft.SharePoint.SPRequestManager", true, true);
          Type spthreadcontext = sprequestmanager.Assembly.GetType("Microsoft.SharePoint.Utilities.SPThreadContext");
          MethodInfo setcontext = spthreadcontext.GetMethod("Set", BindingFlags.Static | BindingFlags.NonPublic);
          Type[] genericArguments = new Type[] { sprequestmanager };
          MethodInfo setcontextgeneric = setcontext.MakeGenericMethod(genericArguments);
          // set the current sprequest manager to null!
          setcontextgeneric.Invoke(null, new object[] { null });
    
          IntPtr p = GetModuleHandle("OWSSVR.DLL");
          FreeLibrary(p);
          string stsadmPath = SPUtility.GetGenericSetupPath("ISAPI");
          p = LoadLibrary(stsadmPath + @"\OWSSVR.DLL");
    
          WriteObject("SharePoint context restarted");
        }
      }
    
    Monday, April 26, 2010 7:40 AM
  • Hi Morten, thanks for this code!

    For others who want to du this: I followed this guide to create the cmdlet:

    http://community.bartdesmet.net/blogs/bart/archive/2008/02/03/easy-windows-powershell-cmdlet-development-and-debugging.aspx

    It works great, just remember to start the 64-bits version of the Visual Studio Command Line when doing the Installutil step.

    Monday, September 27, 2010 1:14 PM
  • If you have a Site Template as a Sandboxed WSP ready, the process of creating a site from a custom site template can be done using PowerShell cmdlets...

    Refer the post below..

    http://rahul-vartak.blogspot.com/2010/12/deploying-sharepoint-2010-sites-using.html

    Section B) -> Note r)

    Wednesday, December 22, 2010 11:34 AM
  • Any chance of a precompiled PowerShell or Console application?

    I'm somehow not able to install any custom Powershell SnapIns via installutil.

    Tuesday, February 22, 2011 3:21 PM
  • (Get-SPFarm).Uncache() worked for me.
    Friday, February 21, 2014 2:21 PM