Thursday, February 16, 2012

Access MasterPage catalog with Powershell

To acces the Masterpage catalog via Powershell, and then use it as a list the following script might help

if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

clear
$siteUrl = "http://yourserver.com"

$masterPageCatalog = "_catalogs/masterPage"

$site = Get-SPSite $siteUrl

$rootWeb = $site.RootWeb;

$folder = $rootWeb.GetFolder($masterPageCatalog)

$masterPageCatList = $rootWeb.Lists[$folder.ParentListId]

#do some list operations on it


$rootWeb.Dispose()
$site.Dispose()

Wednesday, January 25, 2012

Reorder field in existing Contentype withPowershell

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear
$dirURL = "Url of the Content Type"
$contentTypeName = "ContentTypeName"
$fieldInternalName= "Internal name of the field to reorder"
$field0BasedIndex = 8 #Position on the contentytpe this field is wanted

if($site -ne $null){
    $web = $site.RootWeb
    if($web -ne $null){
        $ct = $web.ContentTypes[$contentTypeName]
        if($ct -ne $null){
            write-Host "content Type Found"  -foregroundcolor green
           
            $fields = New-Object 'System.Collections.Generic.List[string]' ;
            [Microsoft.SharePoint.SPFieldLinkCollection]$flinks = $ct.FieldLinks;
            #Read all the InternalNames of the FieldLinks into a list
            foreach($fieldLink in $flinks){
             $fields.Add($ct.Fields[$fieldLink.ID].InternalName);
            }
            if($fields.Contains($fieldInternalName)){           
                write-Host "Field found in Content Type" -foregroundcolor green;
                $indexFound = $fields.IndexOf($fieldInternalName)
                if($indexFound -ne $field0BasedIndex )
                {
                    $fields.RemoveAt($indexFound)
                    #insert or append the field
                    if($field0BasedIndex -le $fields.Count -1){
                        $fields.Insert( $field0BasedIndex,$fieldInternalName)
                    }
                    else{
                        $fields.Add($fieldInternalName);
                    }
                    #Add the array to the reorder
                    $flinks.Reorder($fields.ToArray());
                    #Update the ContentYpe
                    $ct.Update($true)
                 }
            }
            else{
               write-Host "Field not found in Content Type" -foregroundcolor red;
            }
        }
        else{
            write-Host "Content type not found"  -foregroundcolor red
        }
        $web.Update()
        $web.Dispose();
    }
    $site.Dispose();
}

Insert new Site Column in existing Contentype with PowerShell

# Check if Snap-in is loaded
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$dirURL = "Type here the abolute Url to the Sitecollection where the contentytpe is defined"
$contentTypeName = "Traktandum"
$fieldId = [Guid]"{516ae749-c8b4-4a9b-858f-50881aae4165}"  
#fieldId mus match the one on the fieldXML

$site = Get-SPSite -Identity $dirURL
if($site -ne $null){
    $web = $site.RootWeb
    if($web -ne $null){
        #Assign fieldXML variable with XML string for site column
        $fieldXML = '<Field
    Description="$Resources:MyCustom,Field_MyPortalOtherGuest_Description"
    DisplayName="$Resources:MyCustom,Field_MyPortalOtherGuest_DisplayName"
    Hidden="FALSE"
    Name="SomeName"
    Title="SomeName"
    Required="FALSE"
    Sealed ="FALSE"
    Sortable="FALSE"
    List="UserInfo"
    ShowField="ImnName"
    Mult="TRUE"
    Type="UserMulti"
    UserSelectionMode="0"
    UserSelectionScope="0"
    ID="{516ae749-c8b4-4a9b-858f-50881aae4165}"
    Group="$Resources:MyCustom,Field_Group"
    StaticName="SomeName" />'
    
        #Add the field to the web
        $web.Fields.AddFieldAsXml($fieldXML)
        $web.Update()

        $field = $web.AvailableFields[$fieldId]
        $ct = $web.ContentTypes[$contentTypeName]
        if($field -ne $null -and $ct -ne $null){
            #Delete any existing Field with that ID from the ContentType
            $ct.FieldLinks.Delete($fieldId)
            #Update the Contentype includeing childs
            $ct.Update($true, $true)
            #Creat a new SPFieldLink for that field and insert it to the ContentType
            $link = new-object Microsoft.SharePoint.SPFieldLink $field            
            $ct.FieldLinks.Add($link)
            $ct.Update($true, $true)
        }
        $web.Update()
        $web.Dispose()
    }
$site.Dispose()
}

Tuesday, June 14, 2011

Swithc DB to Simple Recovery Mode

http://vstoolsforum.com/blogs/sqlserver/archive/2007/05/11/setting-the-recovery-model-of-a-database-to-simple.aspx

Simple and works perfectly.

Tuesday, May 3, 2011

GAC (Global Assebly Folder) empty- Trick to reenable

As my work is, I have to deploy x-times some assembly in my GAC and test it.

Well once in a while my assembly folder just seems empty, And then I can't  build / deploy my solution anymore.

Here is the trick: (It is not mine.. but the more places it is published, the easier you guy find it:)

Start your  windows services management console (services.msc) and restart the Indexing Service  and everything is back to normal.!

Cool... once agian.. it was not our fault... we where just working!

Tuesday, April 19, 2011

Faster VMWare

I was struggeling with the slowliness of my VMWare Workstation machine. I gave it 12 GB ram and 4 core processors
.
Well it turned out that assigning it more processors, actually made it slower. I reduced the processor to 1
 
I also modified some settings in the config.ini located in c:\programmdata\vmware\vmware workstation\
(This file might not exist if you have the default settings, so make any kind of settings in the VMWare to make the file appear)

installerDefaults.autoSoftwareUpdateEnabled = "yes"
installerDefaults.componentDownloadEnabled = "yes"
installerDefaults.dataCollectionEnabled = "no"
mks.ctlAltDel.ignore = "TRUE"
host.cpukHz = "3000000"
host.noTSC = "TRUE"
ptsc.noTSC = "TRUE"
prefvmx.minVmMemPct = "100"
datastore.name = "local"
defaultVMPath = "E:\DEV_Image"

datastore.localpath = "E:\DEV_Image\"

sched.mem.pshare.enable = "FALSE"
prefvmx.useRecommendedLockedMemSize = "TRUE"
prefvmx.minVmMemPct = "100"
priority.grabbed = "normal"
priority.ungrabbed = "normal"

security.host.ruissl = "FALSE"

mainMem.partialLazySave = "FALSE"
mainMem.partialLazyRestore = "FALSE"
mainMem.useNamedFile = "FALSE"

Isolation.tools.copy.enable = "true"
Isolation.tools.paste.enable = "true"
Isolation.tools.HGFS.disable = "false"

What these settings mean and what they are for you can look up  here
http://sanbarrow.com/vmx/vmx-config-ini.html

Thursday, April 14, 2011

Sharepoint 2010 Rename Assembly of WebPart

I came across of a new issue in SharePoint 2010 today. My client wished the web parts assembly name changed from some name A to some name B.
Well I thought easy, rebuild the web part, verify that the web part file is correct, retract old solution, deploy new solution (Web part is deployed as feature).
Well... When I inserted the web part with the new name everything worked. but all the pages that had the web part already inserted stopped working, saying that the type was not found.
The old web parts where trying to load the old assembly.
After looking and "googling" I didn't find any solution.. So my last resource... change the entry on the Content DB.  Here is the script

UPDATE
[SiteCollection_Content_DB].[dbo].[AllWebParts]SET [tp_WebPartTypeId] ='NewWebPartTypeId',[tp_Assembly] = 'NewAssemblyBaseNamespace, Version=NewVersion, Culture=neutral, PublicKeyToken=newtoken',[tp_Class] = 'NewCompleteClassNameWithNamespace'
[tp_Class]
WHERE [tp_Assembly] = 'OldAssemblyBaseNamespace, Version=OldVersion, Culture=neutral, PublicKeyToken=oldtoken' and = 'OldCompleteClassNameWithNamespace' and[tp_WebPartTypeId] ='OldWebPartTypeId'
To  know the new values I just inserted one web part with the new dll and copied the values form the record I found on the AllWebParts table .


I strongly recommend though to make a backup of your DB!
I actually never touch the DB of SharePoint. I even convinced my client not to change the Assembly name... But still I tried the script in my Dev environment and it seemed to work.




UPDATE: Wow.. why do it the Unsupported way!... there is the supported way: Open your webApplication, loop through all of your sites & webs. Open each page, check if the webpart's in it, if so get the position(s) of the current web part(s). remove it (them) and insert the new webpart(s) with the new definition in the position(s) fetched.