Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

Moving SharePoint Log out of C drive


Topic Moving SharePoint Log out of C drive
Output Quick Win

Step Description
Diagnostic logs: Central Admin > Monitoring > Configure Diagnostic Logging (/_admin/metrics.aspx).
The setting is the “Trace Log” path at the bottom
With PowerShell
Set-SPDiagnosticConfig -LogLocation "D:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS"
Usage logsCentral Admin > Monitoring > Configure web analytics and health data collection (/_admin/LogUsage.aspx).
The setting is the "Log file location" setting.
With PowerShell
Set-SPUsageService -UsageLogLocation "E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS"

Inpersonate a workflow action by code


Inpersonate a workflow action
Even if you use the impersonate step in your worflow the action will run as administrator,
which means that if you copy an item it will have the administrator as owner even if you change the author property during the process, so this will not trigger a new workflow on item created.
The option is to impersonate your custom action, this partial code shows how to do it.

Partial code
       protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                using (SPWeb web = (SPWeb)(__Context.Web))
                {
                    Guid listGuid = new Guid(__ListId);
                    SPList myList = web.Lists[listGuid];
                    SPListItem myItem = myList.GetItemById(__ListItem);
                    SPUserToken spusertoken = web.AllUsers[WorkflowUser].UserToken;
                    using (SPSite destSite = new SPSite(DestinationListUrl, spusertoken))
                    {
                        using (SPWeb destWeb = destSite.OpenWeb())
                        {
                            SPList destList = destWeb.GetList(ProcessStringField(executionContext, DestinationListUrl));
                            SPUser SPAuthor = destWeb.AllUsers[WorkflowUser];                         
                            ProcessActivity(service, myItem, destList, SPAuthor);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return ActivityExecutionStatus.Faulting;
            }
            return ActivityExecutionStatus.Closed;
        }

Launch workflow from outside SharePoint


Launch workflow from outside SharePoint
This used with the windows sheduler allows you to trigger workflow at specific date/time

Sample code
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System.Windows.Forms;

namespace tzSPStartWorkflow
{
    class Program
    {
        static string surl; 
        static string sweb; 
        static string swf; 
        static string sl;
        static string sSource = "tzSPStartWorkflow";
        static string sLog = "Application";

        static void Main(string[] args) {
            try { Execute(args); }
            catch (Exception ex) {
                if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
                EventLog.WriteEntry(sSource, ex.Message);
                EventLog.WriteEntry(sSource, ex.Message, EventLogEntryType.Error, 100);
                Console.WriteLine(ex.Message);
            }
        }


        static void Execute(string[] args)
        {
            //arguments are separated by space
           
            if (args.Length < 0)
            {
                Console.WriteLine("Please enter arguments /s=siteurl /w=website /wf=workflowname /l=listname");
            }
            else {
                for (int i = 0; i < args.Length; i++) 
                { 
                    switch (args[i].Split('=')[0]){
                        case "/s" :
                            surl = args[i].Split('=')[1];

                            break;
                        case "/w" :
                             sweb = args[i].Split('=')[1];
                       
                            break;
                        case "/wf":
                            swf = args[i].Split('=')[1];

                            break;
                        case "/l" :
                            sl = args[i].Split('=')[1];

                            break;
                    }
                }

                if (swf != null && sl != null && surl != null)
                {
                    if (sweb != null)
                    {
                        using (SPSite spsite = new SPSite(surl))
                        {
                            using (SPWeb spweb = spsite.AllWebs[sweb])
                            {
                                SPList splist = spweb.Lists[sl];
                                SPWorkflowManager wfm = spsite.WorkflowManager;
                                SPWorkflowAssociationCollection wfassc = splist.WorkflowAssociations;
                                bool bfind = false;
                                Console.WriteLine(spweb.Url + ": searching for workflow association for " + sl);
                                foreach (SPWorkflowAssociation wfass in wfassc)
                                {
                                    Console.Write("workflow association " + wfass.Name);
                                    if (wfass.Name == swf)
                                    {
                                        Console.WriteLine(" activated ");
                                        //MessageBox.Show(wfass.Name);
                                        Console.WriteLine ( sl + " contains " + splist.Items.Count + " item(s)");
                                        foreach (SPItem spitem in splist.Items)
                                        {
                                            SPWorkflow _workflow = wfm.StartWorkflow(spitem, wfass, wfass.AssociationData, SPWorkflowRunOptions.Asynchronous);
                                            Console.WriteLine("Executing workflow for item " + spitem.ID + " " + spitem[21].ToString());

                                            
                                        }

                                        bfind = true;
                                        
                                    }
                                    else {
                                        Console.WriteLine("disregarded");
                                    }
                                }
                                if (!bfind) { 
                                    //MessageBox.Show("Rien de trouvé !");
                                    string sinfo = " Workflow " + swf + " for list " + sl + " not found !";
                                    Console.WriteLine(sinfo);
                                    if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
                                    EventLog.WriteEntry(sSource, sinfo);
                                    EventLog.WriteEntry(sSource, sinfo, EventLogEntryType.Warning, 110 );
                                    

                                }
                            }
                        }
                    }
                    else {
                        using (SPSite spsite = new SPSite(surl))
                        {
                            using (SPWeb spweb = spsite.RootWeb)
                            {
                                SPList splist = spweb.Lists[sl];
                                SPWorkflowManager wfm = spsite.WorkflowManager;
                                SPWorkflowAssociationCollection wfassc = splist.WorkflowAssociations;
                                Console.WriteLine(spweb.Url + ": searching for workflow association for " + sl);
                                bool bfind = false;
                                foreach (SPWorkflowAssociation wfass in wfassc)
                                {
                                    Console.Write("workflow association " + wfass.Name);
                                    if (wfass.Name == swf)
                                    {
                                        Console.WriteLine(" activated ");
                                        Console.WriteLine(sl + " contains " + splist.Items.Count + " item(s)");
                                        foreach (SPItem spitem in splist.Items)
                                        {
                                            wfm.StartWorkflow(spitem, wfass, wfass.AssociationData, SPWorkflowRunOptions.Asynchronous);
                                            Console.WriteLine("Executing workflow For item " + spitem.ID + " " + spitem[21].ToString());
                                        }

                                        bfind = true;
                                    }
                                    else
                                    {
                                        Console.WriteLine("disregarded");
                                    }
                                }
                                if (!bfind) {
                                    string sinfo = " Workflow " + swf + " for list " + sl + " not found !";
                                    Console.WriteLine(sinfo);
                                    if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog);
                                    EventLog.WriteEntry(sSource, sinfo);
                                    EventLog.WriteEntry(sSource, sinfo, EventLogEntryType.Warning, 110);
                                }


                            }
                        }                  
                    
                    }

                }
                else { Console.WriteLine("Please enter arguments /s=siteurl /w=website /wf=workflowname /l=listname"); }
             
            }

        }
            
    }
       
}

by Category