Need job

Looking for job in Europe. Please contact thru LinkedIn profile.

Thursday, February 21, 2013

Remote upgrade Sharepoint solution with PowerShell

Hello everybody!
Here is a script to upgrade Sharepoint solution on remote server.
U need to allow the remote powershell on the server.
U can replace the sriptblock with any commands. For example Add-Solution)

net use W: /delete
net use W: \\serverName\C$\folder
copy "C:\Users\user\Documents\Visual Studio 2012\Projects\Project\bin\Debug\Project.wsp" W:\
$s = New-PSSession -computerName serverName
Invoke-Command -Session $s -Scriptblock {Add-PSSnapin Microsoft.SharePoint.Powershell;Update-SPSolution -Identity Project.wsp -LiteralPath c:\folder\Project.wsp -GACDeployment}
Remove-PSSession $s

Monday, February 18, 2013

SPItemEventReceiver invoke source detection

How to detect who has update item? User from web browser or web service?
The solution is to store "Label" in thread data slot.
Sync receivers like Adding and Updating executes in the same thread as update code, in my case is web service.

Update Ready-to-use code
using System;
using System.Threading;

namespace Common
{
    public class Threading
    {
        public static void MarkThreadWith(string mark, object val)
        {
            LocalDataStoreSlot store = Thread.GetNamedDataSlot(mark);
            if (store == null)
            {
                store = Thread.AllocateNamedDataSlot(mark);
            }

            Thread.SetData(store, val);
        }

        public static bool CheckThreadFor(string mark, T val) where T : IEquatable
        {
            LocalDataStoreSlot store = Thread.GetNamedDataSlot(mark);
            if (store == null)
            {
                return false;
            }
            try
            {
                T newSlotData = (T)Thread.GetData(store);
                return newSlotData.Equals(val);
            }
            ///wrong thread
            catch (NullReferenceException)
            {
                return false;
            }
        }
    }
}