Monday, October 18, 2010

SharePoint 2010 Modifying UI Version in PowerShell

To change UI version of a SharePoint 2010 site between 2007 and 2010 you can use the following PowerShell code:

$site = Get-SPSite -identity http://{site url}

To 2007 mode:

$site | Get-SPWeb -limit all | ForEach-Object { $_.UIversion = 3; $_.UIVersionConfigurationEnabled = $false; $_.update(); }

To 2010 mode:

$site | Get-SPWeb -limit all | ForEach-Object { $_.UIversion = 4; $_.UIVersionConfigurationEnabled = $false; $_.update(); }

Wednesday, October 6, 2010

PowerShell - Stopping the annoying confirm messages

To stop those annoying confirm messages (i.e. "Are you sure you want to perform this action?") , do this:

$ConfirmPreference = "None"


the end

Looping through all SharePoint 2010 sites with PowerShell

Just needed to loop through all SPS2010 sites and disable and enable a feature. The following PowerShell script does this. (this outputs the site url so I can observe the progress)


$site = Get-SPSite -identity http://{siteurl}


$site | Get-SPWeb -limit all | ForEach-Object { Write-Host $_.Url; Disable-SPFeature -Identity {featureid} -url $_.Url; Enable-SPFeature -Identity {featureid} -url $_.Url }

Friday, October 1, 2010

SharePoint 2010: One or more field types are not installed properly

We started migrating our old SharePoint 2007 sites to SharePoint 2010. It all has been going pretty smoothly, until we found a pesky little problem when creating new subsites:

"One or more field types are not installed properly. Go to the list settings page to delete these fields."

Not much more detail in the ULS logs to describe which field is not installed properly. After some Googling and experimentation the problem was determined to be the changes between SharePoint 2007 and 2010 in the "Relationship List" (http://{siteurl}/Relationships%20List/AllItems.aspx).

Basically to fix the problem, you need to deactivate the SharePoint Server Publishing Infrastructure feature, delete the relationship list and reactivate the SharePoint Server Publishing Infrastructure feature.

The following PowerShell is all you need:

Disable-SPFeature -Identity F6924D36-2FA8-4f0b-B16D-06B7250180FA -Url http://{siteurl}


$site = Get-SPSite -Identity http://{siteurl}

$site.RootWeb.GetList("Relationships List").Delete()


Enable-SPFeature -Identity F6924D36-2FA8-4f0b-B16D-06B7250180FA -Url http://{siteurl}

Wednesday, June 30, 2010

Add Proxy Settings to web.config file of SharePoint with Powershell

I recently needed to modify the default proxy settings in the web.config file of our SharePoint apps. This was done manually to start with, but after many deployments in multiple environments, doing this manually was a chore and often forgotten. I therefore worked on the following PowerShell script that did what we needed.

The following script changes the default proxy settings of:


  <system.net>

    <defaultProxy>

      <proxy autoDetect="true" />

    </defaultProxy>

  </system.net>




to:


  <system.net>

    <defaultProxy useDefaultCredentials="true">

      <proxy usesystemdefault="False" proxyaddress="http://proxy.address" bypassonlocal="True" />

    </defaultProxy>

  </system.net>




Sorry about the crappiness of the cut-and-paste, but this might give you enough help to get you going:


function AddWebConfigModification([Microsoft.SharePoint.Administration.SPWebApplication] $webApp=$(throw 'Parameter -webApp is missing!'),

                                  [string] $name=$(throw 'Parameter -name is missing!'), 

                                  [string] $path=$(throw 'Parameter -path is missing!'), 

                                  [string] $owner=$(throw 'Parameter -owner is missing!'), 

                                  [string] $sequence=0, 

                                  [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType] $modificationType=$(throw 'Parameter -modificationType is missing!'), 

                                  [string] $value=$(throw 'Parameter -value is missing!'))

{

    $modification = New-Object -TypeName "Microsoft.SharePoint.Administration.SPWebConfigModification";

    $modification.Name = $name;

    $modification.Path = $path;

    $modification.Owner = $owner;

    $modification.Sequence = $sequence;

    $modification.Type = $modificationType;

    $modification.Value = $value;    

    $webApp.WebConfigModifications.Add($modification);    

}

 

function AddProxySettings([string]$url=$(throw 'Parameter -url is missing!'))

{

    $webApp = GetWebApp($url);    

    $owner = "GUID_HERE";    

    $ensureAttribute = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureAttribute; 

 

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy" -name "useDefaultCredentials" -value "true";

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "usesystemdefault" -value "False";

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "proxyaddress" -value "http://proxy.address";    

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "bypassonlocal" -value "True";    

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "autoDetect" -value "True";    

 

    [Microsoft.SharePoint.Administration.SPWebService]::ContentService.ApplyWebConfigModifications();

}

Tuesday, September 1, 2009

How to update a SharePoint item while retaining the modified by and date information

In my current project I need to move documents from one document library to another with version history and last modified infomation etc. This works great using SPImport and SPExport.

When the document is moved to the new location, I have a further process of modifiying the values of one of the fields. I needed to do this without modifying the Modified or Modified By fields.

Here is how I did it:

foreach (SPListItem listItem in list.Items)
 {
     var modifiedBy = listItem[SPBuiltInFieldId.Modified_x0020_By];
     var modifiedDate = listItem[SPBuiltInFieldId.Modified];
 
     listItem[_documentTypeFieldName] = GetDocumentType();
     listItem[SPBuiltInFieldId.Modified_x0020_By] = modifiedBy;
     listItem[SPBuiltInFieldId.Modified] = modifiedDate;
     listItem.Update();
 }

BTW. Using Update() creates a new version here. You can not create a new version by using UpdateOverwriteVersion().

Friday, August 21, 2009

Useful code for modifying a Site

I am currently working on a project that needs to support the clients SharePoint implementation. They do everything directly in production, so I have created a site definition to reproduce there site structure. One feature I created to help me with this is basically a web scoped feature that modifies the site with custom changes I want in code (I am a programmer after all!!).

The following code does some interesting stuff:
  • Creates a document library in code
  • Sets the document library title, enables versioning and allows content types in the document library
  • Finds a site content type and associates it with the document library
  • Changes the content type order so it contains just my content type
  • Sets the default page of the site to the default view of the document library
  • Adds some of my content type fields to the default view


public class ProjectSiteReceiver : SPFeatureReceiver
{
    private string _documentLibraryTitle = "Project Documents";
 
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = (SPWeb)properties.Feature.Parent)
        {
            SPListTemplateType templateType = SPListTemplateType.DocumentLibrary;
            Guid listId = web.Lists.Add(_documentLibraryTitle, null, templateType);
 
            SPList list = web.Lists[listId];
            list.Title = _documentLibraryTitle;
            list.EnableVersioning = true;
            list.ContentTypesEnabled = true;
            list.Update();          
 
            SPContentType contentType = web.GetContentType("Project Document");
            SPContentType listContentType = list.ContentTypes.Add(contentType);
            listContentType.Update();
 
            List<SPContentType> contentTypeList = new List<SPContentType>();
            contentTypeList.Add(listContentType);
            list.RootFolder.UniqueContentTypeOrder = contentTypeList;
            list.RootFolder.Update();
 
            SPFolder webFolder = web.RootFolder;
            webFolder.WelcomePage = list.DefaultView.Url;
            webFolder.Update();
 
            SPView view = list.DefaultView;
            view.ViewFields.Add("Project ID");
            view.ViewFields.Add("Project Type");
            view.Update();
        }
    }
 
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = (SPWeb)properties.Feature.Parent)
        {
            SPList list = web.GetListByName(_documentLibraryTitle);
            list.Delete();
        }
    }
 
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {
    }
 
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    {
    }
}




PS: Sorry GetContentType and GetListByName are my own custom extension methods. There isn't much to them really, just better error handling then using the default SharePoint API methods with square brackets.

PPS: You also need to set the welcome page back to the default in the FeatureDeactivating method:

SPFolder webFolder = web.RootFolder;
webFolder.WelcomePage = "default.aspx";
webFolder.Update();