How to execute PowerShell scripts from C# with an execution policy unrestricted

When you are running a PowerShell script, you might get the error:

System.Management.Automation.PSSecurityException : File Test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

 

Solution

In development set the execution policy to Unrestricted.

Set-ExecutionPolicy Unrestricted

http://www.itexperience.net/2008/07/18/file-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system-error-in-powershell/

 

PowerShell script (runs a function from an custom assembly)

Param
(
    [String]$Connection = "",     #The database connectionstring
    [String]$Folder = ""            #The AppPool UserName
)
[System.Reflection.Assembly]::LoadWithPartialName("Ada.Cdf")
$dd = new-object Ada.Cdf.Deployment.DatabaseDeployment
$dd.ExecuteReCreatableScriptsInFolder($Connection, $Folder)

 

Unittest

        [Test]
        [Explicit("Not a unittest")]
        public void RunExecuteReCreatableScriptsInFolder()
        {
            List<string> parameters = new List<string>(){@"Data Source=.;Initial Catalog=TestDB;Integrated Security=SSPI;", @"C:\Temp"};
            
            // Run a PowerShell script with two parameter and set execution policy to Unrestricted
            RunPowershellScript(@"C:\Temp\Test.ps1", parameters);
        }
Function
        private static void RunPowershellScript(string scriptFile, List<string> parameters)
        {
            // Validate parameters
            if (string.IsNullOrEmpty(scriptFile)) { throw new ArgumentNullException("scriptFile"); }
            if (parameters == null) { throw new ArgumentNullException("parameters"); }

            RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
            
            using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
                scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
                Pipeline pipeline = runspace.CreatePipeline();
                Command scriptCommand = new Command(scriptFile);
                Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
                foreach (string scriptParameter in parameters)
                {
                    CommandParameter commandParm = new CommandParameter(null, scriptParameter);
                    commandParameters.Add(commandParm);
                    scriptCommand.Parameters.Add(commandParm);
                }
                pipeline.Commands.Add(scriptCommand);
                Collection<PSObject> psObjects;
                psObjects = pipeline.Invoke();
            }
        } 

 

 

How to show more appointments in the To-Do Bar of Microsoft Outlook 2010

If you wanted to view more appointments in the To-Do Bar of Microsoft Outlook 2007, you could change the default of 3 by right clicking on the To-Do Bar > Views > To-Do Bar > Options > Show Appointments > Number of appointments.

http://blogs.msdn.com/outlook/archive/2008/01/25/make-the-to-do-bar-work-for-you.aspx

 

 

 

 

This option is removed in Microsoft Outlook 2010:

image

 

To show more appointments in the To-Do Bar just increase the viewed space by dragging the divider bar:

image

In cooperation with Marc Valk

Styling, branding SharePoint 2010 based on Active Directory users and groups, no code required, only CSS and Document Permissions

To style a SharePoint 2010 site, based on the current user and his active directory role, I use document permissions on css files in the Style Library.

The goal of the blog post is to Show a a logo of a man, when the current user is member of the Active Directory role [Men] and show a logo of a Woman, when the current user is member of the Active Directory role [Women].

 

Woman (Elise) signs in

image

 

Men (Bas) signs in

image

 

Guide

  • In Active Directory create two roles: [Men] and Women
  • In Active Directory create the users [Bas] and make him member of the role [Men]
  • In Active Directory create the users [Elise] and make her member of the role [Women]
  • In SharePoint 2010 go to Site Actions > Site Permission on you’re site collection top level site
  • Grant Permissions [Full Control] to the Active Directory groups: [Men] and [Women]

image

  • Create two css files on you’re desktop: Men.css and Women.css
  • Men.css contains:

.logo
{
    background-image:url("/Style%20Library/Images/Men.png");
    background-repeat: no-repeat;
    background-position: top left;
    height: 425px;
}

  • Women.css cotains:

.logo
{
    background-image:url("/Style%20Library/Images/Women.png");
    background-repeat: no-repeat;
    background-position: top left;
    height: 425px;
}

 

  • Go to you’re SharePoint top level site, click on Site Actions > Site Settings

image

  • Site Administration > Content and structure

image

  • Upload Women.png and Men.png to the Style Library\Images by dragging and dropping the files (New > Item > Upload Multiple Files…)
  • Create Images folder if it does not exist in the [Style Library]

image

image

  • After upload check the files in

image

  • Upload Men.css and Women.css to the Style Library and check in files
  • Manage permissions on Men.css to include only men (click on Manage Permissions > Stop inherriting permissions), remove user permissions for women
  • Manage permissions on Women.css to include only women(click on Manage Permissions > Stop inherriting permissions), remove user permissions for men

image

  • Open Microsoft SharePoint Designer and open you’re site then click on Master Pages > v4.master
  • Click on Attach Style Sheet, browse for the Men.css stylesheet and click on Import > OK
  • Repeat for Women.css
  • The only thing what this does, is adding the following lines at the end of the <head> tag in the master page

@import url(‘../../Style%20Library/Men.css’);
@import url(‘../../Style%20Library/Women.css’);

image

  • Make sure all persons have read rights on the Master Page Gallery and the v4.master is checked in after you edit the file to import Men.css and Women.css

image 

  • On you’re team site, click in the ribbon on Page > Edit

image

  • Click in the ribbon on Format Text > HTML > Edit HTML Source
  • Add <div class=”logo”></div> to the content editor webpart

image

  • Click on OK then in the ribbon on Save

 

Result, see beginning of this blog post

How to resolve: ListBox UI does not update (refresh) on WCF .NET Ria Services domaincontext async call in Silverlight 3

When you are calling a function on a WCF .NET RIA service, the completed eventhandler will take you back to the UI thread, but if you are binding the ListBox to a List<….> the UI of the Listbox won’t be updated.

Solution: don’t use a List<…> as ItemSource but a System.Collections.ObjectModel.ObservableCollection<…>

Errors after installing Microsoft Office Web Apps

After installing Microsoft Office Web Apps on a SharePoint 2010 server, I got an error when displaying a word (*.docx) file:

 

Error
An unexpected error has occurred.

Troubleshoot issues with Microsoft SharePoint Foundation.

Correlation ID: 848a2b82-a1a4-4faf-895e-1451464fa3b0

Date and Time: 3/10/2010 8:18:03 AM

 

image

 

After activating the Microsoft Office Web Apps feature on the site collection:

image

 

this error was resolved and Microsoft Office Word online started to show, but with a new error:

Word Web App cannot open this document for viewing due to an unexpected error.  To view this document, open it in Microsoft Word.
Error Id: 557be21f-f14d-4c76-9be5-d07f2a04b8f9, 20100310083103

 

This was because you need some additional steps when installing Microsoft Office Web App on a DC, see:

http://sharepoint2010blog.blogspot.com/2010/01/error-in-office-web-apps-when.html

 

But the error was not resolved:

Word Web App cannot open this document for viewing due to an unexpected error.  To view this document, open it in Microsoft Word.
Error Id: 3b5ec400-4f95-495b-b663-320b9b9692be, 20100310124900

image

 

Followed the steps on:

http://blogs.msdn.com/opal/archive/2009/11/16/installation-notice-for-sharepoint-2010-public-beta.aspx

Followed the steps on:

http://dirkvandenberghe.com/archive/2009/12/25/sharepoint-2010-office-web-apps-on-a-single-server-sharepoint-foundation-gt-issues-and-fixes.aspx

Followed the steps on:

http://blah.winsmarts.com/2009-11-SharePoint_2010_Development_Environment_-and-ndash;_Practical_Tips.aspx

 

But the error still was not resolved, I am know rebuilding my server and starting from scratch, following the guide:
http://blogs.architectingconnectedsystems.com/blogs/cjg/archive/2009/11/28/SharePoint-2010-_2D00_-Step-by-Step-Install.aspx

HTTP Error 500.19 – Internal Server Error after installing Microsoft Office Web Apps on a SharePoint 2010 Server

After installing Microsoft Office Web Apps (following the steps on http://technet.microsoft.com/en-us/library/ff431687(office.14).aspx#bkmk_ins_exis_sa) on a Microsoft SharePoint 2010 server, I got a HTTP Error 500.19 when accessing the SharePiont Sites.

 

Error

Config section ‘system.webServer/staticContent’ already defined. Sections must only appear once per config file. See the help topic <location> for exceptions

490:     </staticContent>
491:     <staticContent />
492:   </system.webServer

 

image

 

After removing the double <staticContent /> the site started working again.

How to sett the RootVisual multiple times from code in Silverlight 3

MSDN explains the RootVisual can only be set one time from code:

You can set the value of the RootVisual property only one time from code, although you get its value any number of times.
http://msdn.microsoft.com/en-us/library/system.windows.application.rootvisual(VS.95).aspx

But we can set it to a UserControl that can switch it’s own content, as explained in the blog post:

http://craign.net/2008/03/11/how-to-switch-silverlight-usercontrols/

 

I used this procedure to start with a progressIndicator and after authenticating the user and loading the user properties, showing the masterpage.

Get the DomainService (LINQ to SQL) to load child entities with .NET RIA Services in Silverlight 3

If you have a table with related tables then you can get the child entries by editing the DomainService and the metadata:
Add the include attribute ([Include()]) to the property containing the child table reference and use the DataLoadOptions to specify the child tables to include in the query result view.

In this case a Task table contains a reference to a Project table and the Project table contains a reference to the Customer table:

 

The domainservice class TTSDomainService

// TODO: Consider
// 1. Adding parameters to this method and constraining returned results, and/or
// 2. Adding query methods taking different parameters.
public IQueryable<Task> GetTasks()
{
    // Define the child entries to get 
    DataLoadOptions options = new DataLoadOptions();
    options.LoadWith<Task>(t => t.Project);
    options.LoadWith<Project>(p => p.Customer);
    this.DataContext.LoadOptions = options;

    // Get the tasks ordered by customer, project, task
    return from t in this.DataContext.Tasks
                 orderby t.Project.Customer.Name, t.Project.Name, t.Name
           select t;
}

 

The domainservice meta data class: TTSDomainService.metadata.cs

 

// The MetadataTypeAttribute identifies TaskMetadata as the class
// that carries additional metadata for the Task class.
[MetadataTypeAttribute(typeof(Task.TaskMetadata))]
public partial class Task
{

    // This class allows you to attach custom attributes to properties
    // of the Task class.
    //
    // For example, the following marks the Xyz property as a
    // required field and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz;
    internal sealed class TaskMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private TaskMetadata()
        {
        }

        public Nullable<int> CategoryAbbrID;

        public int CategoryID;

        public Nullable<decimal> EstDuration;

        public Nullable<int> HourType;

        public string Name;

        [Include()]
        public Project Project;

        public int ProjectID;

        public EntitySet<TaskTimeSpan> TaskTimeSpans;
    }
}

 

See:

http://chriswalshie.wordpress.com/2009/08/11/masterdetail-binding-with-net-ria-services-and-the-agdatagrid/