Archive

Archive for the ‘Administration’ Category

SharePoint: Get Installation Directory Path

December 30th, 2011 No comments

    To get the directory path, which SharePoint was installed into, the SPUtility.GetGenericSetupPath method can be used. MSDN says the method returns the full local path for the specified subdirectory. It’s supposed, you pass a relative path of the subdirectory being located inside the SP installation directory. Getting exactly the SP installation directory path, just pass an empty string to the method.

...
using Microsoft.SharePoint.Utilities;
...
string spInstDirPath = SPUtility.GetGenericSetupPath(string.Empty);

In my case the above code sample returns C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14 for SharePoint 2010 and C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12 for SharePoint 2007. To get the path of the Features directory, use the following code snippet:

string spFeaturesDirPath = SPUtility.GetGenericSetupPath(@"template\features");

The GetGenericSetupPath method is accessible only if an application has reference to Microsoft.SharePoint.dll. Let’s assume we develop a SharePoint independent application. How can we get the SP installation directory path in this case? Having analyzed the GetGenericSetupPath method using .Net Reflector, I’ve disclosed that the directory path we are interested in is read from Windows Registry. The SharePoint independent method to get the SP installation directory path and an accompanying method are presented below:

using System;
using System.IO;
using Microsoft.Win32;

...

public static T GetLocalMachineRegistryValue<T>(string path, string valueName, T defaultValue)
{            
    T res = defaultValue;
    try
    {
        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(path))
        {
            if (key != null)
            {
                object valObj = key.GetValue(valueName);
                if (valObj is T)
                    res = (T)valObj;
            }
        }
    }
    catch (Exception)
    {
        // write to log
    }            
    return res;
}

public static string GetSPInstallationDirectoryPath()
{
    const string keyVer2007 = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0";
    const string keyVer2010 = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0";            
    const string valueName  = "Location";

    string res = GetLocalMachineRegistryValue<string>(keyVer2010, valueName, null);

    if(res == null)
        res = GetLocalMachineRegistryValue<string>(keyVer2007, valueName, null);

    return res;
}

The following method is dedicated to get the path of the Features directory:

public static string GetSPFeaturesDirectoryPath()
{
    const string featuresDirPathPattern = @"template\features";

    string res = null;

    string instDirPath = GetSPInstallationDirectoryPath();
    if (instDirPath != null)
        res = Path.Combine(instDirPath, featuresDirPathPattern);

    return res;
}

Here is how you can use these methods:

string spInstallationDir = GetSPInstallationDirectoryPath();
string spFeaturesDir     = GetSPFeaturesDirectoryPath();

Please note, when accessing Registry on x64 Windows from a x32 application, you may stumble upon Registry Reflection and Registry Redirector. Please, take a look at the article to read from the right Registry Key.

Related posts:

SharePoint: How to make error messages more detailed

December 21st, 2011 No comments

    If an unexpected error occurs on a SharePoint environment, by default, you get a meaningless error message as the following: An unexpected error has occurred. Despite the given error message is considered as friendly one for users, we as developers want to have more information to detect the reason. SharePoint is built on ASP.Net technology, it means we can enable the detailed error message and, in addition, displaying of call-stack by changing some settings in a web.config file. We need to find the node customErrors and change its mode to “Off”, which specifies that custom errors are disabled and the detailed errors are shown to the local and remote clients. Then we need to find the node SafeMode and change its CallStack to “true”. CallStack attribute defines whether a call-stack and an exception message are displayed when a system-level exception takes place while ASP.NET processes a request from the local and remote clients. The last step is to save the made changes. The nodes we are interested in are shown below:

Before:

<customErrors mode="On" />
<SafeMode MaxControls="200" CallStack="false" ... >

After:

<customErrors mode="Off" />
<SafeMode MaxControls="200" CallStack="true" ... >

The required web.config file containing main settings locates in \inetpub\wwwroot\wss\VirtualDirectories\<SHAREPOINT APP PORT NUMBER>. For SharePoint 2007 it’s usually enough to make changes in the given file only. As for SharePoint 2010, the changes also should be applied to a number of additional web.config files in 14 hive (\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14) of SharePoint 2010. If you make a search for web.config inside the 14 folder you likely will find more than 30 different files. I recommend firstly to make changes in web.config files locating in \14\TEMPLATE\ADMIN and \14\TEMPLATE\LAYOUTS and then check whether you still get not detailed error message. If so, continue making the changes file by file until the message will get more detailed.

Related posts:

SharePoint 2010: Some issues during solution package deploying

December 17th, 2011 No comments

    Deploying a .wsp-file on SharePoint 2010, you may stumble over the next error message:
Install-SPSolution : Admin SVC must be running in order to create deployment timer job.

How have I got it? In SharePoint 2010 Management Shell I had successfully performed the following command to upload my SP2010 solution package into the system:

Add-SPSolution "C:\WSPDeployment\My2010Solution.wsp"

Then I had tried to run the next one to deploy the uploaded solution:

Install-SPSolution -Identity My2010Solution.wsp -GACDeployment -AllWebApplications

and after that I received the mentioned error.

Now, how to fix it. It’s needed to go to Administrative Tools -> Services and manually start the service called SharePoint 2010 Administration. Not to have such error in the future, change service’s Startup type to Automatic.

SharePoint 2010 Administration service

Having made this, I run Install-SPSolution once again and received a new error:
A deployment or retraction is already under way for the solution “My2010Solution.wsp”, and only one deployment or retraction at a time is supported.

It looked like an appropriate deployment job was added to the list of timer jobs, but the attempt to execute the job failed due to unstarted SharePoint 2010 Administration service. I found a solution here. We need to cancel the deployment job by performing the next steps:

  1. Grab the id of the job by running the command: stsadm -o enumdeployments. Yep, the old friend, stsadm helps us out πŸ™‚ Look through the result, find the required deployment job and grab the id. The result should look like
    C:\...red\Web Server Extensions\14\BIN>stsadm -o enumdeployments
    
    <Deployments Count="1">
       <Deployment JobId="2de92a5f-6c70-4325-ac2c-293a34dd9c67">
          <Title>Microsoft SharePoint Foundation Solution Deployment for "My2010Solution.wsp"</Title>
          <Type>Deployment</Type>
          <State>Pending</State>
          <Schedule>12/16/2011 3:47 PM</Schedule>
          <File>My2010Solution.wsp</File>
          <ServerType>Front-end Web server</ServerType>
          <Target>http://mySPServer/</Target>
       </Deployment>
    </Deployments>
    
  2. Using the found id, cancel the job by means of the command stsadm-o canceldeployment-id β€œfound Job id”. For example, in my case it’s
    stsadm -o canceldeployment -id "2de92a5f-6c70-4325-ac2c-293a34dd9c67"

    If it succeed you get “Operation completed successfully” message.

  3. Run again stsadm -o enumdeployments to make sure that the given deployment job disappeared

Ok, now you can repeat deployment of your solution, run Install-SPSolution again. The issues was overcome in my case.

SharePoint: How to shrink transaction log file

August 3rd, 2011 No comments

     While restoring a production database in my local environment, first of all I shrink the database transaction log file, because it usually takes up a lot of space. I use SQL Server 2008, and in order to get the transaction log shrunk, I can suggest swithing the database recovery model from FULL to SIMPLE and back. To accomplish that:

  1. Switch the database recovery model to SIMPLE using the following command:
    • ALTER DATABASE <Your Database Name> SET RECOVERY SIMPLE;
  2. Shrink the database or transaction log file using Microsoft SQL Server Management Studio:
    Shrink Transaction Log
    Or use the following command to shrink the transaction log file, for example, to 5 MB:

    • DBCC SHRINKFILE (<Your Database Name>_Log, 5);
  3. Switch the database recovery model back to FULL using the following command:
    • ALTER DATABASE <Your Database Name> SET RECOVERY FULL;

If it’s not a production database, you can actually skip step 3, leaving the database recovery model in SIMPLE state; the Transaction log in this case is not going to grow. But keep in mind that some people might complain that it causes problems when attempting to delete SiteCollection from SharePoint Central Administration. I’ve never faced such issues, but you may want to keep FULL recovery model just in case.

Related posts:

SharePoint: The specified SPContentDatabase has been upgraded to a newer version

April 6th, 2011 No comments

     Yesterday I was trying to restore the Web-application from fresh SQL-backup of database of production server. Doing that I used the algorithm described in one of my previous post “How to Restore a SharePoint Site from a Database Backup”. I restored backup into a new database, then I tried to create a new Web-application connected to the restored database, however, the undertaking failed, because I received the following error:

The specified SPContentDatabase Name=WSS_CONTENT_PROD Parent=SPDatabaseServiceInstance has been upgraded to a newer version of SharePoint. Please upgrade this SharePoint application server before attempting to access this object.

The specified SPContentDatabase has been upgraded to a newer version of SharePoint. Please upgrade this SharePoint application server before attempting to access this object

     According to the error some update has been deployed on the production server and has led to the version increase. The obvious solution was to upgrade the local environment. First of all, I went to Central Administration->Operations->Servers in Farm and found out the local current version of SharePoint, it was 12.0.0.6504.

Where to take look at version

     In the same time the current version on production was 12.0.0.6510. By means of the page “How To find the SharePoint version” I discovered the cumulative update I should have set up to bring local version to production version. I downloaded the required update and installed. Unfortunately, despite the successful installation, the local version didn’t change at all and the error still remained during a new Web-application creation. When the standard steps don’t help, we need a trick. I tried to find where the restored database contains the information about version. The table Versions struck my eye:

SELECT [VersionId]
      ,[Version]
      ,[Id]
      ,[UserName]
      ,[TimeStamp]
      ,[FinalizeTimeStamp]
      ,[Mode]
      ,[ModeStack]
      ,[Updates]
      ,[Notes]
  FROM [WSS_CONTENT_PROD].[dbo].[Versions]

Table Versions

     As you can see, we have one record where version is greater than local version (12.0.0.6510 > 12.0.0.6504). I replaced 12.0.0.6510 with 12.0.0.6504 using the following sql-command:

Update [WSS_CONTENT_PROD].[dbo].[Versions] 
SET [Version] = '12.0.0.6504' 
where [Version] = '12.0.0.6510'

     After this trick I was able to create a new Web-application connected to the restored database without errors. Of course, Microsoft doesn’t welcome any handmade changes in content database and you should avoid doing them, but if you don’t have a choice you can attempt at your own risk πŸ™‚ In my case it has saved much time and life energy for me πŸ™‚

Related posts: