Wednesday, November 23, 2011

Parsing XML and databinding using jsRender and jsViews (RIP jQuery Templates)

I have always been a huge fan of the very flexible ASP.NET ListView control for creating templates and databinding nested data collections. Recently though I have been making a stronger effort to work with client side technologies like jQuery and client friendly data formats like JSON and JSONP. Thanks to the hard work of Microsoft’s Boris Moore, a new technology called jQuery Templates was put into beta as an official plugin and a CDN was created in jQuery and Microsoft. Unfortunately after spending some time studying the framework, I learned that it had been deprecated, “rebranded” as jsRender and jsViews, and a tentative schedule to add it to the jQuery UI framework (at least the jsRender portion). Fortunately, the new framework still has support, has improved performance, and a syntax that hasn’t changed too much. This is really a useful technology, so hopefully it will get adopted soon as a first class jQuery plugin or independent framework.
jsRender Databinding SyntaxThe first line initializes the template object. The object is then rendered with the data and appended to an html element.
$("#tblTemplate").template("tblTemplate");
$("#tblBod").append($.render(obj, "tblTemplate"));

Saturday, September 17, 2011

Installing SharePoint 2010 on Windows 8

Just for kicks I got Windows 8 Preview installed today and thought I'd see if I could get SharePoint 2010 installed as well. Fortunately, the setup instructions are pretty much identical to the ones used for Windows 7. The important thing to remember is that you need to make sure IIS uses .NET 2.0 instead of 4.0.
  • Install SQL Server 2008. I used R2, but you can use any version listed in the requirements.
  • Ensure all of the required IIS features are installed through Programs and Features. The easiest way to do this is to use the script that Microsoft provides. You can do this by copying the following into a Command Prompt.

Monday, June 6, 2011

Add every installed web part to a single SharePoint page

Here is a code snippet that won't be used for normal day usage, but something is very helpful for testing web part UI and branding. This could also be used for performance benchmarking and exploring or demonstrating different web parts in a development environment. By default, the code will alphabetize the web parts by title and then adds them to the top web part zone.You can easily update the code to filter out specific web parts, update web part properties, populate specific web part zones.

InstructionsThe first thing you’ll want to do is increase the maximum web parts per zone. The default is set to 50, but you can increase it by editing your web.config.

Friday, June 3, 2011

Create a favorites icon (favicon) for your SharePoint 2010 site

With SharePoint 2010, Microsoft has provided a new control called SPShortcutIcon. Sadly, if you are trying to use this within a feature deployment and would like to use relative paths using $SPUrl variables, you will run into some issues. This is because the SPShortcut.IconUrl is of type URI and the $SPUrl variable cannot be cast properly. Fortunately, there is a work around. It’s a bit ugly, but instead of using the control, you can use Literal controls to render the proper html link control.
SPShortcut usage– This is hardcoded, so won’t work with feature deployments.
<SharePoint:SPShortcutIcon runat="server" IconUrl="/Style Library/Waffles/favicon.ico"/>
SPShortcut usage with $SPUrl – This will result in the following error: Unable to cast object of type 'System.String' to type 'System.Uri'.
<
SharePoint:SPShortcutIcon runat="server" IconUrl="<% $SPUrl:~sitecollection/Style Library/Acme/favicon.ico %>" />
Solution


<
asp:Literal runat="server" Text="&lt;link rel='shortcut icon' href='" /><asp:Literal runat="server" Text="<% $SPUrl:~sitecollection/Style Library/Acme/favicon.ico %>" /><asp:Literal runat="server" Text="' type='image/vnd.microsoft.icon' /&gt;" />
Google.com’s favorite icon

image
SharePoint 2010, Favorites Icons (FavIcons) and SPUrl (SPUrlExpressionBuilder)Credit goes to Joel for this tip. Unfortunately, I could only get to his post via Google cache.http://joelblogs.co.uk/2010/11/09/sharepoint-2010-favorites-icons-favicons-and-spurl-spurlexpressionbuilder/

Wednesday, June 1, 2011

Get SharePoint audit details across the farm for specific users

This code snippet gets all audit details for specific users across an entire SharePoint 2007/2010 farm and saves them to a CSV file. I have also added a method to include a file extension column. This comes in handy as you will likely want to filter out web extensions like aspx, html, css, etc., picture formats, and folders. In order to get any data, auditing will have to have been enabled at the Site Collection level through site settings or programmatically at the item level. Keep in mind that this will take some time if your AuditData tables are very large, so in this case you will want to make further refinements by filtering by site collection, list, or list item. Site collections can be filtered out by tweaking the loop statements below, while you can filter out list or list items by updating the RestrictToList or RestrictToListItem methods of the SPAuditQuery object.
Helpful Links
Configure audit settings for a site collections
http://office.microsoft.com/en-us/sharepoint-server-help/configure-audit-settings-for-a-site-collection-HA010099726.aspx
Custom Auditing in SharePoint
http://msdn.microsoft.com/en-us/magazine/cc794261.aspx

Instructions
Create a c# Console Application in Visual Studio, add a reference to Microsoft.SharePoint, and then add the following code to Program.cs. This will work with SharePoint 2007 and 2010 as long as you reference the correct assembly. Don’t forget to make sure your Visual Studio project’s Platform Target is Any CPU or x64.

Wednesday, May 25, 2011

Determine auditing configuration details for all of your SharePoint site collections

I recently had a customer whose SharePoint audit tables had getting very large. They wanted to know which site collections had auditing enabled so they could meet with their end users to determine if auditing was really necessary. Here is a code snippet for finding out which SharePoint sites have auditing enabled. The code will loop through all site collections in the farm and then create a CSV file with auditing configuration details.
InstructionsCreate a c# Console Application in Visual Studio, add a reference to Microsoft.SharePoint, and then add the following code to Program.cs. This will work with SharePoint 2007 and 2010 as long as you reference the correct assembly. Don’t forget to make sure your Visual Studio project’s Platform Target is Any CPU or x64.


Wednesday, April 13, 2011

Create Rounded Corners And Gradient Images On The Fly!

I am often tasked with taking a mockup and branding a site. This usually involves a custom master page with tweaks to the CSS for the layout and color scheme. It also involves the creation of images used to compliment the UI, the holy grail for some being gradient images and rounded corners. For the amateur graphic designer like myself, this often becomes a tedious task that involves repetitive tweaks to an image. I once had a client tell me that I had the wrong shade of “pumpkin”. (Depending on your disposition, this could result in an act of either running full speed into a brick wall or rocking yourself to sleep every night.)
Enter the poorly named Web Designer Tool! After some experimentation with the System.Drawing and System.Drawing.Drawing2D .NET 2.0 classes, I figured out a way to generate these images on the fly. Now I can rapidly create rounded corners and gradients and don't have to sweat it if a client needs a color or hue adjusted. The rounded corners include many options, including width, height, gradient and solid colors, radius corner size, and border. I also provide a preview feature that integrates the images into a sample html page and launches it in your default browser. I have successfully tested this in IE 6.0 ~ 8.0, Chrome 10.0, and Firefox 4.0.
Note: Although this will fly in the face of CSS purists, I have decided to use a simple table based layout for the preview html. I do make an effort to use DIV based layouts for web design, but in this case I have yet to find approach that works flawlessly across browsers types and versions or that integrates nicely with SharePoint. If you are that CSS guru, I welcome you to take these images and come up with a better approach. If you are successful, I would love to try out your code. You are also welcome to use my Shapes.cs class below and build your own tool. Life will be much easier once CSS 3 has reached enough market penetration and we can simply use the border-radius property.


Friday, February 11, 2011

Custom Control with Connecting Lines using WPF / Silverlight

Recently I was developing a migration tool that incorporates two TreeView controls and allows a user to draw lines between controls. After a bit of a struggle, I finally came up with a solution that works pretty well. The control inherits from the Grid control and uses drag and drop event handlers to draw connecting lines between controls of the same type. Because it is a Grid control, it also gives you the ability to add multiple controls and style it as you wish. In this example, I simply embedded a border control with some rounded corners and line gradients. Within the Border control, I simply added a TextBlock with the necessary text. A random color algorithm (courtesy of Philosophil Blog) determines what color is used.

Custom Properties
ConnectedControl – When two controls are connected, they are able to reference each other through this property.
ConnectedLine – This contains a reference to the line that is added.
AllowMultipleConnections – This determines if the control to connect to multiple items.
ConnectionType – This is an enum that determines whether a control can connect to other controls based on their connection type. The values Both, Source, and Target. Both is the default and says that the control can connect to any other control. Source controls can only connect to Target controls and vice versa.
LineThickness – This determines how thick the connecting line is.

Steps
In order to use this control, you simply need to create a class called ConnectingControl.cs and copy and paste the code below your WPF project. (Of course, you will need to update your namespace.) In order to add the control to your page, simply add a reference to your local assembly and then add the control. At the very least, you will also need to nest another control like a TextBlock or Image. I have included the source code for my MainWindow.xaml below.

Screenshot

ConnectingLines

MainWindow.xaml

<Window x:Class="ConnectingLines.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ConnectingLines"
Title="Connecting Lines" Height="350" Width="525">
<
Window.Resources>
<
Style x:Key="ConnectingGridBorderSource" TargetType="Border">
<
Setter Property="Background" Value="{DynamicResource BoxBrushSource}" />
<
Setter Property="CornerRadius" Value="0,35,0,35" />
<
Setter Property="Margin" Value="0" />
</
Style>
<
Style x:Key="ConnectingGridBorderTarget" TargetType="Border">
<
Setter Property="Background" Value="{DynamicResource BoxBrushTarget}" />
<
Setter Property="CornerRadius" Value="0,35,0,35" />
<
Setter Property="Margin" Value="0" />
</
Style>
<
Style x:Key="ConnectingGridSource" TargetType="local:ConnectingControl">
<
Setter Property="Margin" Value="10" />
<
Setter Property="AllowMultipleConnections" Value="False" />
<
Setter Property="ConnectionType" Value="Source" />
<
Setter Property="LineThickness" Value="3" />
</
Style>
<
Style x:Key="ConnectingGridTarget" TargetType="local:ConnectingControl">
<
Setter Property="Margin" Value="10" />
<
Setter Property="AllowMultipleConnections" Value="False" />
<
Setter Property="ConnectionType" Value="Target" />
<
Setter Property="LineThickness" Value="3" />
</
Style>
<
Style x:Key="ConnectingGridTextBlock" TargetType="TextBlock">
<
Setter Property="Foreground" Value="AliceBlue" />
<
Setter Property="FontSize" Value="24" />
<
Setter Property="FontWeight" Value="Bold" />
<
Setter Property="VerticalAlignment" Value="Center" />
<
Setter Property="HorizontalAlignment" Value="Center" />
</
Style>
<
LinearGradientBrush x:Key="BoxBrushSource" StartPoint="0,0" EndPoint="0,1">
<
LinearGradientBrush.GradientStops>
<
GradientStop Color="AliceBlue" Offset="0.0" />
<
GradientStop Color="Green" Offset="0.4" />
</
LinearGradientBrush.GradientStops>
</
LinearGradientBrush>
<
LinearGradientBrush x:Key="BoxBrushTarget" StartPoint="0,0" EndPoint="0,1">
<
LinearGradientBrush.GradientStops>
<
GradientStop Color="AliceBlue" Offset="0" />
<
GradientStop Color="DarkBlue" Offset=".4" />
</
LinearGradientBrush.GradientStops>
</
LinearGradientBrush>
</
Window.Resources>
<
Grid>
<
Grid.ColumnDefinitions>
<
ColumnDefinition />
<
ColumnDefinition />
<
ColumnDefinition />
</
Grid.ColumnDefinitions>
<
Grid.RowDefinitions>
<
RowDefinition />
<
RowDefinition />
<
RowDefinition />
</
Grid.RowDefinitions>
<
local:ConnectingControl Style="{StaticResource ConnectingGridSource}" Grid.Row="0" Grid.Column="0">
<
Border Style="{StaticResource ConnectingGridBorderSource}">
<
TextBlock Text="Banana" Style="{StaticResource ConnectingGridTextBlock}" />
</
Border>
</
local:ConnectingControl>
<
local:ConnectingControl Style="{StaticResource ConnectingGridSource}" Grid.Row="1" Grid.Column="0">
<
Border Style="{StaticResource ConnectingGridBorderSource}">
<
TextBlock Text="Spinach" Style="{StaticResource ConnectingGridTextBlock}" />
</
Border>
</
local:ConnectingControl>
<
local:ConnectingControl Style="{StaticResource ConnectingGridSource}" Grid.Row="2" Grid.Column="0">
<
Border Style="{StaticResource ConnectingGridBorderSource}">
<
TextBlock Text="Feta" Style="{StaticResource ConnectingGridTextBlock}" />
</
Border>
</
local:ConnectingControl>
<
local:ConnectingControl Style="{StaticResource ConnectingGridTarget}" Grid.Row="0" Grid.Column="2">
<
Border Style="{StaticResource ConnectingGridBorderTarget}">
<
TextBlock Text="Cheese" Style="{StaticResource ConnectingGridTextBlock}" />
</
Border>
</
local:ConnectingControl>
<
local:ConnectingControl Style="{StaticResource ConnectingGridTarget}" Grid.Row="1" Grid.Column="2">
<
Border Style="{StaticResource ConnectingGridBorderTarget}">
<
TextBlock Text="Fruit" Style="{StaticResource ConnectingGridTextBlock}" />
</
Border>
</
local:ConnectingControl>
<
local:ConnectingControl Style="{StaticResource ConnectingGridTarget}" Grid.Row="2" Grid.Column="2">
<
Border Style="{StaticResource ConnectingGridBorderTarget}">
<
TextBlock Text="Vegetable" Style="{StaticResource ConnectingGridTextBlock}" />
</
Border>
</
local:ConnectingControl>
</
Grid>
</
Window>


ConnectingControl.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ConnectingLines
{
public class ConnectingControl : Grid
{
#region PROPERTIES
public enum Connection { Both, Source, Target };
private ConnectingControl ConnectedControl { get; set; }
private Line ConnectedLine { get; set; }

new private bool AllowDrop { get; set; }

#region DEPENDENCY PROPERTIES
public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

public static readonly DependencyProperty AllowMultipleConnectionsProperty = DependencyProperty.Register("AllowMultipleConnections", typeof(bool), typeof(ConnectingControl));
public static readonly DependencyProperty ConnectionTypeProperty = DependencyProperty.Register("ConnectionType", typeof(Connection), typeof(ConnectingControl));
public static readonly DependencyProperty LineThicknessProperty = DependencyProperty.Register("LineThickness", typeof(double), typeof(ConnectingControl));

public bool AllowMultipleConnections
{
get
{
return (bool)GetValue(AllowMultipleConnectionsProperty); }
set
{
SetValue(AllowMultipleConnectionsProperty, value);
NotifyPropertyChanged("AllowMultipleConnections");
}
}

public Connection ConnectionType
{
get { return (Connection)GetValue(ConnectionTypeProperty); }
set
{
SetValue(ConnectionTypeProperty, value);
NotifyPropertyChanged("ConnectionType");
}
}

public double LineThickness
{
get
{
double thickness = (double)GetValue(LineThicknessProperty);
if (thickness == 0)
{
thickness = 3;
}
return thickness;
}
set
{
SetValue(LineThicknessProperty, value);
NotifyPropertyChanged("LineThickness");
}
}
#endregion
#endregion

#region
CONSTRUCTOR AND INITIALIZER
public ConnectingControl()
{
this.DragEnter += new DragEventHandler(CustomButton_DragEnter);
this.Drop += new DragEventHandler(CustomButton_Drop);
this.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(CustomButton_PreviewMouseLeftButtonDown);
}

protected override void OnInitialized(System.EventArgs e)
{
base.AllowDrop = true;
base.OnInitialized(e);
PreviewMouseLeftButtonDown += new MouseButtonEventHandler(CustomButton_PreviewMouseLeftButtonDown);
Drop += new System.Windows.DragEventHandler(CustomButton_Drop);
DragEnter += new System.Windows.DragEventHandler(CustomButton_DragEnter);
}
#endregion

#region
EVENT HANDLERS AND METHODS
protected void CustomButton_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
ConnectingControl item = (ConnectingControl)sender;
DataObject dragData = new DataObject("source", (ConnectingControl)sender);
DragDrop.DoDragDrop((ConnectingControl)sender, dragData, DragDropEffects.Move);
}

protected void CustomButton_DragEnter(object sender, System.Windows.DragEventArgs e)
{
if (!e.Data.GetDataPresent("source") || sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}

protected void CustomButton_Drop(object sender, System.Windows.DragEventArgs e)
{
if (e.Data.GetDataPresent("source"))
{
ConnectingControl source = e.Data.GetData("source") as ConnectingControl;
ConnectedControl = source;
ConnectControls();
}
}

private void ConnectControls()
{
bool connect = false;
if (this.ConnectionType == Connection.Both)
{
connect = true;
}
else
{
if (this.ConnectionType == ConnectedControl.ConnectionType)
{
connect = false;
}
else
{
connect = true;
}
}
if (connect)
{
Panel parent = GetRoot(this);
Line line = new Line() { StrokeThickness = LineThickness, Stroke = GetRandomBrush() };
GeneralTransform sourceTransform = ConnectedControl.TransformToVisual(parent);
Point sourcePoint = sourceTransform.Transform(new Point(0, 0));
GeneralTransform targetTransform = this.TransformToVisual(parent);
Point targetPoint = targetTransform.Transform(new Point(0, 0));
if (sourcePoint.X < targetPoint.X)
{
line.X1 = sourcePoint.X + ConnectedControl.ActualWidth;
line.Y1 = sourcePoint.Y + ConnectedControl.ActualHeight / 2;
line.X2 = targetPoint.X;
line.Y2 = targetPoint.Y + this.ActualHeight / 2;
}
else
{
line.X1 = targetPoint.X + this.ActualWidth;
line.Y1 = targetPoint.Y + this.ActualHeight / 2;
line.X2 = sourcePoint.X;
line.Y2 = sourcePoint.Y + ConnectedControl.ActualHeight / 2;
}
if (parent.GetType() == typeof(Grid))
{
line.SetValue(Grid.ColumnSpanProperty, ((Grid)parent).ColumnDefinitions.Count);
line.SetValue(Grid.RowSpanProperty, ((Grid)parent).RowDefinitions.Count);
}
if (!AllowMultipleConnections)
{
if (ConnectedLine != null)
{
parent.Children.Remove(ConnectedLine);
}
if (ConnectedControl != null)
{
if (ConnectedControl.ConnectedLine != null)
{
GetRoot(this).Children.Remove(ConnectedControl.ConnectedLine);
}
}
}
ConnectedLine = line;
ConnectedControl.ConnectedLine = line;
parent.Children.Add(line);
}
}
#endregion

#region
MISC METHODS
private Panel GetRoot(FrameworkElement child)
{
var parent = child.Parent as FrameworkElement;
if (parent == null)
{
if (child is Window)
{
if (((Window)child).Content.GetType().BaseType == typeof(Panel))
{
return ((Window)child).Content as Panel;
}
else
{
throw new Exception("The root content element is an unexpected type. It should be a Panel instead of a " +
((Window)child).Content.GetType().BaseType.ToString() + ".");
}
}
else
{
throw new Exception("The root element is an unexpected type. It should be a Window instead of a" +
child.GetType().ToString() + ".");
}
}
return GetRoot(parent);
}

//COLOR GENERATOR CODE Courtesy of: http://philosophil.spaces.live.com/blog/cns!7E55D8EFA2AEE5D6!201.entry
private Brush GetRandomBrush()
{
Random randomColor = new Random();
SolidColorBrush brush = new SolidColorBrush();
double r, g, b;
double lightness = randomColor.NextDouble() * 0.5 + 0.4; // not too dark nor too light
double hue = randomColor.NextDouble() * 360.0; // full hue spectrum
double saturation = randomColor.NextDouble() * 0.8 + 0.2; // not too grayish
HSLtoRGB(hue, saturation, lightness, out r, out g, out b);
brush.Color = System.Windows.Media.Color.FromRgb((byte)(r * 255.0), (byte)(g * 255.0), (byte)(b * 255.0));
return brush;
}

public static void HSLtoRGB(double hue, double saturation, double luminance, out double red, out double green, out double blue)
{
double q;
double p;
if (luminance < 0.5)
{
q = luminance * (1.0 + saturation);
}
else
{
q = luminance + saturation - (luminance * saturation);
}
p = 2 * luminance - q;
double hk = hue / 360.0;
double tr = hk + 1.0 / 3.0;
double tg = hk;
double tb = hk - 1.0 / 3.0;
tr = Normalize(tr);
tg = Normalize(tg);
tb = Normalize(tb);
red = ComputeColor(q, p, tr);
green = ComputeColor(q, p, tg);
blue = ComputeColor(q, p, tb);
}

private static double ComputeColor(double q, double p, double tc)
{
if (tc < 1.0 / 6.0)
{
return p + ((q - p) * 6.0 * tc);
}
if (tc < 0.5)
{
return q;
}
if (tc < 2.0 / 3.0)
{
return p + ((q - p) * 6.0 * (2.0 / 3.0 - tc));
}
return p;
}

private static double Normalize(double tr)
{
if (tr < 0)
{
return tr + 1.0;
}
if (tr > 1.0)
{
return tr - 1.0;
}
return tr;
}
#endregion
}
}

A simple WPF fading popup control

Steps:

1.) Create a WPF Project in Visual Studio.  I named mine FadingPopup. (Note: You can use 3.5 or 4.0, but you will need to use 4.0 if you want to take advantage of the animation EasingFunctions.)
2.) Add a User Control with the following code:

FadingPopupControl.xaml

<UserControl x:Class="FadingPopup.FadingPopupControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" BorderThickness="0" Margin="0">
<
UserControl.Resources>
<
Storyboard x:Name="StatusFader" x:Key="StatusFader" Completed="StatusFader_Completed">
<
DoubleAnimation Storyboard.TargetName="popupBackground" Storyboard.TargetProperty="Opacity" From="0.7" To="0" BeginTime="0:0:0" Duration="0:0:1.5">
<
DoubleAnimation.EasingFunction>
<
ExponentialEase Exponent="10" EasingMode="EaseIn" />
</
DoubleAnimation.EasingFunction>
</
DoubleAnimation>
</
Storyboard>
</
UserControl.Resources>
<
Popup Name="popup" Placement="Center" PopupAnimation="Fade" AllowsTransparency="True">
<
Grid Background="Transparent">
<
Grid Name="popupBackground" Background="Black" Grid.Column="0" Grid.Row="0" />
<
Label Name="popupLabel" DockPanel.Dock="Bottom" HorizontalAlignment="Center" Foreground="White" VerticalAlignment="Center" Background="Transparent" Grid.Column="0" Grid.Row="0"/>
</
Grid>
</
Popup>
</
UserControl>



FadingPopupControl.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace FadingPopup
{
public partial class FadingPopupControl : UserControl
{
Window ParentWindow { get; set; }
public FadingPopupControl()
{
InitializeComponent();
}

protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
}

public void ShowDialogBox(Window parentWindow, string message)
{
ParentWindow = parentWindow;
popupLabel.Content = message;
Storyboard StatusFader = (Storyboard)Resources["StatusFader"];
ParentWindow.IsEnabled = false;
FrameworkElement root = (FrameworkElement)ParentWindow.Content;
this.Height = root.ActualHeight;
this.Width = root.ActualWidth;
//TODO: Determine why there is 1 pixel extra whitespace.
//Tried playing with Margins and Alignment to no avail.
popup.Height = root.ActualHeight + 1;
popup.Width = root.ActualWidth + 1;
popup.IsOpen = true;
StatusFader.Begin(popupBackground);
}

void StatusFader_Completed(object sender, EventArgs e)
{
popup.IsOpen = false;
ParentWindow.IsEnabled = true;
}
}
}


 


3.) In the MainWindow.xaml, add a Button with a PreviewMouseLeftButtonDown event handler.
4.) Add a local reference and then your popup control.
5.) In your button event handler, initiate the popup.

MainWindow.xaml


<Window x:Class="FadingPopup.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FadingPopup"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
Title="MainWindow" Height="350" Width="525">
<
Grid>
<
Button x:Name="Test1" Height="40" Width="80" Content="Test1" Grid.Column="0" AllowDrop="False" PreviewMouseLeftButtonDown="Test1_PreviewMouseLeftButtonDown" />
<
local:FadingPopupControl x:Name="popup" />
</
Grid>
</
Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Input;

namespace FadingPopup
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Test1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
popup.ShowDialogBox(Test1, "The is a sample message...");
}
}
}

Screenshots



Fading2


This fades out in 1.2 seconds


Fading

Tuesday, February 1, 2011

PowerShell for SharePoint on Steroids!

I created this script as an effort to quickly deploy an entire site structure into a DEV and QA (and potentially PROD) environment. I also wanted something I could easily edit on the fly. You can copy and paste the source code and save it as a ps1 file or download the code. You must run it on a SharePoint server with farm administrator privileges.

This script includes the following functions:

- Creating Web Application
- Creating Site Collections
- Creating Subsites
- Setting Master Pages recursively.
- Adding Features
- Adding Site Columns
- Creating Lists
- Adding folders to Lists
- Adding Site Column to Lists
- Creating Views
- Enabling/Disabling Publishing, Checkin, and Approvals
- Creating Sample Items for Links, Announcements, and Calendar lists with permissions.
- Creating Publishing Pages
- Creating Groups with default owners
- Set Group permissions for subsites
- Add Content Query Web Parts (CQWP) for site and subsite lists.

Tools
You can easily use Notepad to edit this file, but I highly recommend PowerGUI.
It it is free and has a great interface and provides things like debugging, Intellisense, formatting,
and plug-ins. Power GUI: http://www.powergui.org

Developer Notes: I am not a PowerShell pro by any stretch of imagination and am still learning the intricacies of the language. There is no use of classes, collections, and exception handling is sparse. Some of this is due to my ignorance on the subject. That being said, I have no doubt there are areas that can be written more efficiently.

CreateSamplePublishingPortal Function

Description
This function will use all of the scripts functions and create an entire web application and site collection with subsites, site columns, lists, sample list items, and permissions. You can easily comment out or change

Parameters
If you choose false for either, ensure that all of the variables in the function are correct.
$createWebApplication - Boolean to determine if Web Application is created.
$createSiteCollection - Boolean to determine if Site Collection is created.
$env - Determines the environment. Valid values are: "dev", "qa", or "prod".
Variables within the function must be updated to use this functionality.

Global Variables
Before running the script, you will want to make sure and update all Global Variabes in this script
to ensure they will work with your environment. You can also comment out parts of the script that you don't want to run.

Lists
The multichoice "Site Visibility" site column is added to all lists. It include the values
"Private Team", "Public Team", and "Intranet".
- Corresponding views are created for each "Site Visibility" value.
- A "public" folder added to each list and read permissions added to allow users outside of the private site.
- Sample items are also added to the lists and every combination of "Site Visibility" is used. If the
value for the item has a "Site Visibility" that contains "Public Team" or "Intranet" it is moved into the "public" folder.
This allows permissions to stay at the folder level and not the item level.
- Note: A workflow still needs to be created to ensure that when items are created or changed they go to the public folder.

Permissions
All departmental private team sites disinherit the top level permissions so that unique permissions can be created. For example, Human Resources would include the following groups:

Group Name Permission
HR Owners Full Control
HR Members Contribue
HR Visitors Read

The script also applies a default owner for all groups. This is an array variable that can be updated before running the script.

Aggregate Views
Aggregate views using Content Query Web Parts (CQWP) are added to the Intranet and Public departmental team sites.

Extras
The script also does a few extra things like disabling publishing and approval, changing master page to v4.master, enabling Team Collaboration Features. I did this for a little more flexibility, especially for DEV and QA mode. These can all be easily disabled or changed back by using one of the functions in this file. For example, if you already have a web application, you can comment out "CreateWebApplication".

Execution Time
My notebook is not virtual, has an Intel i7 an 8 GB RAM. It takes around 8 minutes to run the entire script. Of that 8 minutes. Rough creation times (in seconds): Web Applicatoin: 110 s. / Site collection: 75 s. / Site: 30 s.

Site Hierarchy
The following site hierarchy is created with this function. You can easily change this hierarchy by updating the variables in the function.

Intranet (Web Application)
Intranet Site Collection *(See notes below)
Departments (Publishing Site)
Private (Publishing Site)
Finance (Publishing Site)
Announcements (Announcements List)
News (News List)
Team (Calendar Calendar)
I Want To (Links List)
Staff Roles (Publishing Page)
Training (Publishing Page)
New Hires (Publishing Page)
Human Resources (Publishing Site)
Announcements (Announcements List)
News (News List)
Team (Calendar Calendar)
I Want To (Links List)
Staff Roles (Publishing Page)
Training (Publishing Page)
New Hires (Publishing Page)
Information Technology Publishing Site
Announcements (Announcements List)
News (News List)
Team (Calendar Calendar)
I Want To (Links List)
Staff Roles (Publishing Page)
Training (Publishing Page)
New Hires (Publishing Page)
Marketing (Publishing Site)
Announcements (Announcements List)
News (News List)
Team (Calendar Calendar)
I Want To (Links List)
Staff Roles (Publishing Page)
Training (Publishing Page)
New Hires (Publishing Page)
Public Publishing Site
Finance (Publishing Site) **(See notes below)
Human Resources (Publishing Site) **(See notes below)
Information Technology (Publishing Site) **(See notes below)
Marketing (Publishing Site) **(See notes below)

* Intranet Home page includes CQWPs that point to Lists from all departmental Private departmental sites. These are filtered by "Site Visibility" column where the value contains "Intranet".

** TODO: Departmental Public Home page includes CQWPs that aggregate Lists from corresponding Private departmental site.
These are filtered by "Site Visibility" column where the value contains "Public Team".

Download Link
SharePoint 2010 Portal Builder.ps1

Source Code
(Unfortunately, blogspot destroyed any decent formatting I had, so I recommend downloading the file.)

#--------------------------------Sharepoint 2010 Portal Builder-------------------------------------------------------
#
# Description: I created this script as an effort to deploy an entire site structure
# into a DEV and QA (and potentially PROD) environment up and running quick.
# I also wanted something I could easily edit on the fly.
#
# This script includes the following functions:
#
# - Creating Web Application
# - Creating Site Collections
# - Creating Subsites
# - Setting Master Pages recursively.
# - Adding Features
# - Adding Site Columns
# - Creating Lists
# - Adding folders to Lists
# - Adding Site Column to Lists
# - Creating Views
# - Enabling/Disabling Publishing, Checkin, and Approvals
# - Creating Sample Items for Links, Announcements, and Calendar lists with permissions.
# - Creating Publishing Pages
# - Creating Groups with default owners
# - Set Group permissions for subsites
# - Add Content Query Web Parts (CQWP) for site and subsite lists.
#
# Tools: You can easily use Notepad to edit this file, but I highly recommend PowerGUI.
# It it is free and has a great interface and provides things like debugging, Intellisense, formatting,
# and plug-ins. Power GUI: http://www.powergui.org
#
# Developer Notes: I am not a PowerShell pro by any stretch of imagination and am still learning the intricacies of the language.
# There is no use of classes, collections, and exception handling is sparse. Some of this is due to my ingorance
# on the subject. That being said, I have no doubt there are areas that can be written more efficiently.
#
# Author: John Livingston
# Website: http://johnlivingstontech.blogspot.com
# Created Date: 2/1/2011
# Updated Date: 2/1/2011
#
#--------------------------------CreateSamplePublishingPortal Function-------------------------------------------------------
#
# Description: This function will use all of the scripts functions and create an entire web application and site collection
# with subsites, site columns, lists, sample list items, and permissions. You can easily comment out or change
#
# Parameters: If you choose false for either, ensure that all of the variables in the function are correct.
# $createWebApplication - Boolean to determine if Web Application is created.
# $createSiteCollection - Boolean to determine if Site Collection is created.
# $env - Determines the environment. Valid values are: "dev", "qa", or "prod".
# Variables within the function must be updated to use this functionality.
#
# Global Variables: Before running the script, you will want to make sure and update all Global Variabes in this script
# to ensure they will work with your environment. You can also comment out parts of the script that you don't want to run.
#
# Lists: - The multichoice "Site Visibility" site column is added to all lists. It include the values
# "Private Team", "Public Team", and "Intranet".
# - Corresponding views are created for each "Site Visibility" value.
# - A "public" folder added to each list and read permissions added to allow users outside of the private site.
# - Sample items are also added to the lists and every combination of "Site Visibility" is used. If the
# value for the item has a "Site Visibility" that contains "Public Team" or "Intranet" it is moved into the "public" folder.
# This allows permissions to stay at the folder level and not the item level.
# - Note: A workflow still needs to be created
# to ensure that when items are created or changed they go to the public folder.
#
# Permissions: All departmental private team sites disinherit the top level permissions so that unique permissions can be created.
# For example, Human Resources would include the following groups:
#
# Group Name Permission
# HR Owners Full Control
# HR Members Contribue
# HR Visitors Read
#
# The script also applies a default owner for all groups. This is an array variable that can be updated before running the script.
#
# Aggregate Views: Aggregate views using Content Query Web Parts (CQWP) are added to the Intranet and Public departmental team sites.
# and aggregate views CQWP that use list views
# created from the private team sites.
# It also does a few extra things like disabling publishing and approval, changing master page to v4.master,
# enabling Team Collaboration Features.
#
# Extras: The script also does a few extra things like disabling publishing and approval, changing master page to v4.master,
# enabling Team Collaboration Features. I did this for a little more flexibility, especially for DEV and QA mode.
# These can all be easily disabled or changed back by using one of the functions in this file. For example, if you already
# have a web application, you can comment out "CreateWebApplication".
#
# Execution Time: My notebook is not virtual, has an Intel i7 an 8 GB RAM. It takes around 8 minutes to run the entire script. Of that 8 minutes.
# Rough creation times (in seconds): Web Applicatoin: 110 s. / Site collection: 75 s. / Site: 30 s.
#
#
# Site Hierarchy: The following site hierarchy is created with this function.
# You can easily change this hierarchy by updating the variables in the function.
#
# Intranet Web Application
# Intranet Site Collection *(See notes below)
# Departments Publishing Site
# Private Publishing Site
# Finance Publishing Site
# Announcements Announcements List
# News News List
# Team Calendar Calendar
# I Want To Links List
# Staff Roles Publishing Page
# Training Publishing Page
# New Hires Publishing Page
# Human Resources Publishing Site
# Announcements Announcements List
# News News List
# Team Calendar Calendar
# I Want To Links List
# Staff Roles Publishing Page
# Training Publishing Page
# New Hires Publishing Page
# Information Technology Publishing Site
# Announcements Announcements List
# News News List
# Team Calendar Calendar
# I Want To Links List
# Staff Roles Publishing Page
# Training Publishing Page
# New Hires Publishing Page
# Marketing Publishing Site
# Announcements Announcements List
# News News List
# Team Calendar Calendar
# I Want To Links List
# Staff Roles Publishing Page
# Training Publishing Page
# New Hires Publishing Page
# Public Publishing Site
# Finance Publishing Site **(See notes below)
# Human Resources Publishing Site **(See notes below)
# Information Technology Publishing Site **(See notes below)
# Marketing Publishing Site **(See notes below)
#
# * Intranet Home page includes CQWPs that point to Lists from all departmental Private departmental sites.
# These are filtered by "Site Visibility" column where the value contains "Intranet".
#
# ** TODO: Departmental Public Home page includes CQWPs that aggregate Lists from corresponding Private departmental site.
# These are filtered by "Site Visibility" column where the value contains "Public Team".

function CreateWebApplication ($webApplicationName, $appPoolUserName, $appPoolPassword, $portNumber, $serverName, $timeZoneId)
{
#--------------CONCATENATED VARIABLES (UPDATE FORMAT IF NECESSARY)-----------------
$appPoolName = $webApplicationName + "_AppPool" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$databaseServer = $webApplicationName + "_ContentDB" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$rootDirectory = "C:\Inetpub\wwwroot\wss\VirtualDirectories\" + $portNumber ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------CREATING WEB APP-----------------
Write-Host "Creating Web Application - "$webApplicationName
$farm = [microsoft.sharepoint.administration.spfarm]::local
$WebAppBuilder = new-object microsoft.sharepoint.administration.SPWebApplicationBuilder($farm)
$WebAppBuilder.ApplicationPoolId = $appPoolName
$WebAppBuilder.ApplicationPoolUsername = $appPoolUserName
$WebAppBuilder.ApplicationPoolPassword = $appPoolPassword
$WebAppBuilder.IdentityType = [Microsoft.SharePoint.Administration.IdentityType]::SpecificUser
$WebAppBuilder.Port = $portNumber
$WebAppBuilder.ServerComment = $webApplicationName
$WebAppBuilder.CreateNewDatabase = $true
$WebAppBuilder.DatabaseServer = $serverName
$WebAppBuilder.DatabaseName = $databaseServer
$WebAppBuilder.RootDirectory = $rootDirectory
$WebAppBuilder.UseSecureSocketsLayer = $false
$WebAppBuilder.AllowAnonymousAccess = $false
$webapp = $WebAppBuilder.Create()
$webapp.ProvisionGlobally()
$webapp.DefaultTimeZone = $timeZoneId
$webapp.Update()
Write-Host "Finished Creating Web Application - "$webApplicationName
}

function CreateSiteCollection($siteName, $siteUrl, $siteOwner, $siteTemplate, $removeLibraryApprovalWorkflow, $disableCheckoutModeration, $addTeamCollaborationFeature)
{
Write-Host "Creating Site Collection - "$siteName
New-SPSite -Name $siteName -Url $siteUrl -OwnerAlias $siteOwner -Template $siteTemplate
Write-Host "Finished Creating Site Collection - "$siteName
if($disableCheckoutModeration)
{
EnableMasterPageModerationCheckout $siteUrl $disableCheckoutModeration
}
if($removeLibraryApprovalWorkflow)
{
$listNames = @("Pages", "Documents", "Images") #Publishing Site Default Lists
RemoveApprovalWorkflows $siteUrl "" $listNames "Page Approval"
}
if($addTeamCollaborationFeature)
{
$teamCollaborationGuid = New-Object System.Guid("00bfea71-4ea5-48d4-a4ad-7ea5c011abe5") #Team Collaboration Feature (Allows for more content types)
AddWebFeature $siteUrl "" $teamCollaborationGuid
}
}

function CreateWeb($siteUrl, $webUrl, $mappedPath, $webName, $webOwner, $webTemplate, $removeLibraryApprovalWorkflow, $addTeamCollaborationFeature)
{
Write-Host "Creating Web - "$webName
New-SPWeb -Name $webName -Url ($siteUrl+$webUrl) -Template $webTemplate
if($removeLibraryApprovalWorkflow)
{
$listNames = @("Pages", "Documents", "Images") #Publishing Site Default Lists
if($removeLibraryApprovalWorkflow)
{
RemoveApprovalWorkflows $siteUrl $webRelativeUrl $listNames "Page Approval"
}
if($addTeamCollaborationFeature)
{
$teamCollaborationGuid = New-Object System.Guid("00bfea71-4ea5-48d4-a4ad-7ea5c011abe5") #Team Collaboration Feature (Allows for more content types)
AddWebFeature $siteUrl ($mappedPath+$webUrl) $teamCollaborationGuid
}
}
Write-Host "Finished Creating Web - "$webName
}

function AddWebFeature($siteUrl, $webUrl, $featureGuid)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$web.Features.Add($featureGuid)
Write-Host "Finished Adding Feature - ("+$featureGuid+") "+$siteUrl$webUrl
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Creating List - "$listName "for" $webUrl
}

function AddContentByQueryWebPart($siteUrl, $webPartSourceWebUrl, $webPartWebUrl, $queryListWebUrl, $webPartPageUrl, $queryListName, $webPartTitle, $filterField, $filterValue, $filterDisplayValue, $itemLimit, $zoneId, $zoneIndex)
{
$listId = ""
try
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($queryListWebUrl)
$list = $web.Lists[$queryListName]
$listId = $list.ID
}
finally
{
$web.Dispose()
}
try
{
$web = $site.OpenWeb($webPartSourceWebUrl)
$web.AllowUnsafeUpdates = $true
$file = $web.GetFile($webPartPageUrl)
try
{
$list = $web.Lists["Pages"]
if($list.ForceCheckout -eq $true)
{
$file.CheckOut()
$file.Update()
}
$mgr = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$cqwp = New-Object Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart
$cqwp.DisplayColumns = 1
$cqwp.FeedEnabled = $false
$cqwp.Filter1ChainingOperator = 0#Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart.FilterChainingOperator.And
$cqwp.Filter1IsCustomValue = $false
$cqwp.FilterByAudience = $false
$cqwp.FilterDisplayValue1 = $filterDisplayValue
$cqwp.FilterField1 = $filterField
$cqwp.FilterIncludeChildren1 = $false
$cqwp.FilterOperator1 = "Contains"#1#Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart.FilterFieldQueryOperator.Contains
$cqwp.FilterType1 = "MultiChoice"
$cqwp.FilterValue1 = $filterValue
$cqwp.GroupByDirection = 1#Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart.SortDirection.Desc
$cqwp.GroupStyle = "DefaultHeader"
$cqwp.ItemLimit = $itemLimit
$cqwp.ItemStyle = "Default"
$cqwp.ListGuid = $listId
$cqwp.ListName = $queryListName
$cqwp.PlayMediaInBrowser = $true
$cqwp.ServerTemplate = "104"
$cqwp.ShowUntargetedItems = $false
$cqwp.SortBy = "Created"
$cqwp.SortByDirection = 1#Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart.SortDirection.Desc
$cqwp.SortByFieldType = "DateTime"
$cqwp.Title = $webPartTitle
$cqwp.UseCache = $true
$cqwp.UseCopyUtil = $true
$cqwp.WebUrl = $webPartWebUrl
$mgr.AddWebPart($cqwp, $zoneId, $zoneIndex)
if($list.ForceCheckout -eq $true)
{
$file.Update()
$file.CheckIn("")
$file.Publish("")
$file.Approve("")
}
$web.Update()
Write-Host "Finished Adding Content Query Web Part - "$siteUrl$webPartWebUrl" from "$queryListWebUrl "("$queryListName")"
}
finally
{
$mgr.Dispose()
}
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function CreateList($siteUrl, $webUrl, $listName, $listDescription, $listTemplate)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$web.Lists.Add($listName, $listDescription, $listTemplate)
$web.Update()
Write-Host "Finished Creating List - "$webUrl"/"$listName
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Creating List - "$listName "for" $webUrl
}

function AddListFolder($siteUrl, $webUrl, $listName, $folderName, $isList)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
if($isList)
{
$folder = $list.Items.Add($list.RootFolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $folderName)
}
else
{
$folder = $list.RootFolder.SubFolders.Add($folderName).Item
}
if(!($folder.ModerationInformation -eq $null))
{
$folder.ModerationInformation.Status = [Microsoft.SharePoint.SPModerationStatusType]::Approved
}
$folder.Update()
Write-Host "Finished Creating Folder - "$folderName" on "$siteUrl$webUrl"/"$listName"/"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function AddListSiteColumn($siteUrl, $webUrl, $listName, $fieldName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
$field = $site.RootWeb.Fields[$fieldName]
$list.Fields.Add($field)
$list.Update()
$view = $list.DefaultView
$view.ViewFields.Add($fieldName)
$view.Update()
Write-Host "Finished Adding Site Column - "$fieldName" to "$siteUrl$webUrl"/"$listName"/"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function AddListView($siteUrl, $webUrl, $listName, $viewName, $fieldNames, $camlQuery, $rowLimit, $paged, $defaultView, $showFolders)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
$colViews = $list.Views
$collViewFields = New-Object System.Collections.Specialized.StringCollection
foreach($fieldName in $fieldNames)
{
$collViewFields.Add($fieldName)
}
$strQuery = ($camlQuery)
$colViews.Add($viewName, $collViewFields, $strQuery, $rowLimit, $paged, $defaultView)
if($showFolders -eq $false)
{
$view = $list.Views[$viewName]
$view.Scope = [Microsoft.SharePoint.SPViewScope]::Recursive
$view.Update()
}
Write-Host "Finished Adding List View - "$viewName" to "$siteUrl$webUrl"/"$listName"/"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function SetListFolderPermissions($siteUrl, $webUrl, $listName, $folderName, $groupName, $roleDefinition)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
$folder = $list.RootFolder.SubFolders[$folderName].Item
$folder.BreakRoleInheritance($true)
$web.AllowUnsafeUpdates = $true
$group = $web.SiteGroups[$groupName]
$roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group)
$roleAssignment.RoleDefinitionBindings.Add($web.RoleDefinitions[$roleDefinition])
$folder.RoleAssignments.Add($roleAssignment)
$folder.Update()
Write-Host "Finished Adding Permissions to folder "$folderName" on "$siteUrl$webUrl"/"$listName" ("$groupName" - "$roleDefinition")"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function CreateSiteColumnMultiChoice($siteUrl, $fieldName, $fieldChoices, $defaultValue, $group, $required)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb()
try
{
$web.Fields.Add($fieldName, [Microsoft.SharePoint.SPFieldType]::MultiChoice, $required)
$field = $web.Fields[$fieldName]
foreach($fieldChoice in $fieldChoices)
{
$field.Choices.Add($fieldChoice)
}
$field.DefaultValue = $defaultValue
$field.Group = $group
$field.Update()
Write-Host "Finished Adding Site Column ("$fieldName") to web "$siteUrl
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function SetRoleInheritance($siteUrl, $webUrl, $breakInheritance)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
if($breakInheritance)
{
$web.BreakRoleInheritance($breakInheritance)
}
else
{
$web.ResetRoleInheritance()
}
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Setting Inheritance - "$siteUrl$webUrl " (Break Inheritance - "$breakInheritance")"
}

function AddSiteGroup($siteUrl, $groupName, $defaultUser, $ownerGroup, $description)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb()
try
{
$web.SiteGroups.Add($groupName, $web.SiteGroups[$ownerGroup], $web.SiteUsers[$defaultUser], $description)
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Adding Site Group - "$groupName" to "$siteUrl
}

function AddGroupPermission($siteUrl, $webUrl, $groupName, $roleDefinitionName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$web.AllowUnsafeUpdates = $true
$group = $site.RootWeb.SiteGroups[$groupName]
$roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $site.RootWeb.RoleDefinitions[$roleDefinitionName]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Adding Group Permission - "$groupName "("$roleDefinitionName") to "$siteUrl$webUrl
}

function RemoveGroupPermission($siteUrl, $webUrl, $groupName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$web.AllowUnsafeUpdates = $true
$group = $site.RootWeb.SiteGroups[$groupName]
$web.RoleAssignments.Remove($group)
$web.Update()
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Removing Group Permission - "$groupName "from" $siteUrl$webUrl
}

function CreatePublishingPage($siteUrl, $webUrl, $pageFileName, $pageName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pageLayouts = $publishingWeb.GetAvailablePageLayouts()
$currPageLayout = $pageLayouts[0]
$pages = $publishingWeb.GetPublishingPages()
$newPage = $pages.Add($pageFileName, $currPageLayout)
$newPage.Title = $pageName
$newPage.Update()
$newPage.CheckIn("")
$newPage.ListItem.File.Approve("")
Write-Host "Finished Creating Page - "$pageName "for" $webUrl
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function RemoveApprovalWorkflows($siteUrl, $webUrl, $listNames, $workFlowName)
{
foreach($listName in $listNames)
{
RemoveApprovalWorkflow $siteUrl $webUrl $listName $workFlowName
}
}

function RemoveApprovalWorkflow($siteUrl, $webUrl, $listName, $workFlowName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
if(!($list -eq $null))
{
$wa = $list.WorkflowAssociations.GetAssociationByName($workFlowName, [System.Globalization.CultureInfo]::CurrentCulture)
if(!($wa -eq $null))
{
Write-Host "Removing" $wa.Name "from" $listName
$list.WorkflowAssociations.Remove($wa)
Write-Host "Finished Removing Workflow "$workFlowName" from "$siteUrl$webUrl"/"$listName
}
}
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function AddApprovalWorkflow($siteUrl, $webUrl, $listName, $workflowTemplateName, $workflowName, $workflowTaskName, $workflowHistoryName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
if(!($list -eq $null))
{
$wfTemp = $web.WorkflowTemplates.GetTemplateByName($workflowTemplateName, [System.Globalization.CultureInfo]::CurrentCulture);
$wf = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($wfTemp, $workflowName, $web.Lists[$workflowTaskName], $web.Lists[$workflowHistoryName]);
$list.WorkflowAssociations.Add($wf)
$list.DefaultContentApprovalWorkflowId = $wf.Id
$list.Update()
Write-Host "Finished Adding Workflow "$workflowTemplateName" to "$siteUrl$webUrl"/"$listName
}
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function EnableMasterPageModerationCheckout($siteUrl, $enable)
{
$site = Get-SPSite $siteUrl
try
{
$list = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::MasterPageCatalog)
$list.EnableModeration = $enable
$list.ForceCheckout = $enable
$list.Update()
Write-Host "MasterPage Moderation and Checkout on "$siteUrl" set to "$enable
}
finally
{
$site.Dispose()
}
}

function UpdateMasterPageUrl($siteUrl, $webUrl, $masterPageUrl, $inheritMasterPage)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$web.CustomMasterUrl = $masterPageUrl
if(!($web -eq $null))
{
if($inheritMasterPage)
{
$web.AllProperties["__InheritsCustomMasterUrl"] = "True";
}
else
{
$web.AllProperties["__InheritsCustomMasterUrl"] = "False";
}
}
Write-Host "MasterPage Updated for "$siteUrl"/"$webUrl" (Custom URL:"$masterPageUrl" / Inheritance:"$inheritMasterPage")"
$web.Update()
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function AddSampleAnnouncements($siteUrl, $webUrl, $webName, $listName)
{
$title = "Announcement"
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
$listItems = $list.Items
$itemCount = ($listItems.Count - 1)
for($i=0; $i -lt $itemCount; $i++)
{
if(!($item.FileSystemObjectType -eq [Microsoft.SharePoint.SPFileSystemObjectType]::Folder))
{
$listItems.Delete($i)
}
}
#Private Team Item
$item = $list.Items.Add()
$item["Title"] = ($webName + " " + $title + " - 1")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Public Team Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 2")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Public Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 3")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team and Public Team
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 4")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Public Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team, Public Team, and Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 5")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Public Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Public Team and Itranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 6")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Public Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team and Intranet Iteam
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 7")
$item["Body"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
Write-Host "Finished Adding Sample Announcements to "$siteUrl$webUrl" ("$listName")"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function AddSampleLinks($siteUrl, $webUrl, $webName, $listName)
{
$title = "Link"
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
#Private Team Item
$list = $web.Lists[$listName]
$item = $list.Items.Add()
$item["Title"] = ($webName + " " + $title + " - 1")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 1"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Public Team Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 2")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 2"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Public Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 3")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 3"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team and Public Team
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 4")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 4"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Public Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team, Public Team, and Intranet Team
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 5")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 5"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Public Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Public Team and Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 6")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 6"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Public Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team and Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 7")
$urlValue = New-Object Microsoft.SharePoint.SPFieldUrlValue
$urlValue.Description = $webName + " - " + $title + " 7"
$urlValue.Url = "http://www.google.com"
$item["URL"] = $urlValue
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
Write-Host "Finished Adding Sample Links to "$siteUrl$webUrl" ("$listName")"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function AddSampleCalendarEvents($siteUrl, $webUrl, $webName, $listName)
{
$title = "Event"
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
#Private Team Site Item
$list = $web.Lists[$listName]
$item = $list.Items.Add()
$item["Title"] = ($webName + " " + $title + " - 1")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Public Team Site Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 2")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Public Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 3")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team and Public Team Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 4")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Public Team")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team, Public Team, and Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 5")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Public Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Public Team and Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 6")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Public Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
#Private Team and Intranet Item
$folder = $list.RootFolder.SubFolders["public"]
$item = $list.Items.Add($folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File)
$item["Title"] = ($webName + " " + $title + " - 7")
$item["Description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$siteVisValue = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$siteVisValue.Add("Private Team")
$siteVisValue.Add("Intranet")
$item["Site Visibility"] = $siteVisValue
$item.Update()
Write-Host "Finished Adding Sample Events to "$siteUrl$webUrl" ("$listName")"
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
}

function CreateSamplePublishingPortal($createWebApplication, $createSiteCollection, $env)
{
#--------------Web App Variables-----------------
$webApplicationName = "Intranet"
$appPoolUserName = "CORP\Administrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$appPoolPassword = ConvertTo-securestring "pass@word1" -asplaintext -force ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$portNumber = "80" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$serverName = "SPSERVER" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$timeZoneId = 13 #Pacific Time (US and Canada) ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Site Collection Variables-----------------
$siteName = "Intranet"
$siteManagedPath = "/sites/Intranet" #"" if root level. Otherwise, something like "/sites/PortalName"
$siteUrl = "http://" + $serverName + ":" + $portNumber + $siteManagedPath
$siteOwner = "CORP\Administrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$siteTemplate = Get-SPWebTemplate "BLANKINTERNETCONTAINER#0" #BLANKINTERNETCONTAINER#0 - Publishing Portal STS#0 Team Site
$siteColumnName = "Site Visibility"
$siteColumnDefaultValue = "Private Team"
$siteColumnGroup = "CompanyX Columns"
#--------------Web Variables-----------------
$webOwner = "CORP\Administrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$webTemplate = Get-SPWebTemplate "BLANKINTERNETCONTAINER#0"
$disableCheckoutModeration = $true
$removeLibraryApprovalWorkflow = $true
$addTeamCollaborationFeature = $true
$webTitles = @(
"Departments",
"Private",
"Finance",
"Human Resources",
"Information Systems",
"Marketing"
"Public",
"Finance",
"Human Resources",
"Information Systems",
"Marketing"
)
$webUrls = @(
("/dep"),
("/dep/private"),
("/dep/private/fi"),
("/dep/private/hr"),
("/dep/private/is"),
("/dep/private/mkt"),
("/dep/public"),
("/dep/public/fi"),
("/dep/public/hr"),
("/dep/public/is"),
("/dep/public/mkt")
)
$teamUrls = @(
($siteManagedPath + "/dep/private/fi"),
($siteManagedPath + "/dep/private/hr"),
($siteManagedPath + "/dep/private/is"),
($siteManagedPath + "/dep/private/mkt"),
($siteManagedPath + "/dep/public/fi"),
($siteManagedPath + "/dep/public/hr"),
($siteManagedPath + "/dep/public/is"),
($siteManagedPath + "/dep/public/mkt")
)
$privateTeamUrls = @(
($siteManagedPath + "/dep/private/fi"),
($siteManagedPath + "/dep/private/hr"),
($siteManagedPath + "/dep/private/is"),
($siteManagedPath + "/dep/private/mkt")
)
$privateTeamRelativeUrls = @(
("~sitecollection/dep/private/fi"),
("~sitecollection/dep/private/hr"),
("~sitecollection/dep/private/is"),
("~sitecollection/dep/private/mkt")
)
$privateTeamNames = @(
"Finance",
"HR",
"IS",
"Marketing"
)
$publicTeamUrls = @(
($siteManagedPath + "/dep/public/fi"),
($siteManagedPath + "/dep/public/hr"),
($siteManagedPath + "/dep/public/is"),
($siteManagedPath + "/dep/public/mkt")
)
$siteGroups = @(
"Finance Owners",
"Finance Members",
"Finance Visitors",
"HR Owners",
"HR Members",
"HR Visitors",
"IS Owners",
"IS Members",
"IS Visitors",
"Marketing Owners",
"Marketing Members",
"Marketing Visitors"
)
$siteGroupOwners = @(
($siteName + " Owners"),
"Finance Owners",
"Finance Owners",
($siteName + " Owners"),
"HR Owners",
"HR Owners",
($siteName + " Owners"),
"IS Owners",
"IS Owners",
($siteName + " Owners"),
"Marketing Owners",
"Marketing Owners"
)
#NOTE: THIS ASSUMES YOU HAVE THESE USERS - MAKE ARRAY EMPTY IF NOT ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$siteGroupDefaultUsers = @(
"FIOwner",
"FIMember",
"FIVisitor",
"HROwner",
"HRMember",
"HRVisitor",
"ISOwner",
"ISMember",
"ISVisitor",
"MTKOwner",
"MTKMember",
"MTKVisitor"
)
#--------------MasterPage Variables-----------------
$masterPageUrl = $siteManagedPath + "/" + "_catalogs/masterpage/v4.master" #nightandday.master is default. v4.master is more minimalist
#--------------List Variables-----------------
$linksTemplate = [Microsoft.SharePoint.SPListTemplateType]::Links
$announcementsTemplate = [Microsoft.SharePoint.SPListTemplateType]::Announcements
$calendarTemplate = [Microsoft.SharePoint.SPListTemplateType]::Events
$listNameTemplates = @($announcementsTemplate, $linksTemplate, $announcementsTemplate, $calendarTemplate)
$listNames = @("Announcements", "I Want To", "News", "Team Calendar")
$pageUrl = "/pages/default.aspx"
#--------------List View Variables-----------------
$fieldNames = @("Name", "Site Visibility")
$privateCamlQuery = "Private Team"
$publicCamlQuery = "Public Team"
$intranetCamlQuery = "Intranet"
$listSiteVisibilityTitle = ""
$listSiteVisibilityChoices = @("Private Team", "Public Team", "Intranet")
$listViewColumnFilterName = "Site_x0020_Visibility"
$listViewColumnPublicFilterValue = "Public Team"
$listViewColumnIntranetFilterValue = "Intranet"
$listViewItemLimit = 10;
$listViewZoneId = "TopZone"
#--------------List Folder Variables-----------------
$listFolderName = "public"
$listFolderPermissionGroup = ($siteName + " Visitors")
$listFolderPermission = "Read"
#--------------Publishing Page Variables-----------------
$publishingPages = @("NewHires.aspx", "StaffRoles.aspx", "Training.aspx")
#--------------Permissions Variables-----------------
$permissionPermissionNames = @(
"Finance",
"HR",
"IS",
"Marketing"
)
#--------------UPDATE VARIABLES DEPENDING ON ENVIRONMENT-----------------
#You can add more variables from above down here. I only included common ones that would likely change between environments.
#Expected Values: dev, qa, or prod
switch($env)
{
"dev"
{
#--------------Web App Variables-----------------
$webApplicationName = "DEVIntranet"
$appPoolUserName = "CORP\DEVAdministrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$appPoolPassword = ConvertTo-securestring "pass@word1" -asplaintext -force ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$portNumber = "80" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$serverName = "DEVSPSERVER" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Site Collection Variables-----------------
$siteName = "DEVIntranet"
$siteManagedPath = "/sites/Intranet" #"" if root level. Otherwise, something like "/sites/PortalName"
$siteUrl = "http://" + $serverName + ":" + $portNumber + $siteManagedPath
$siteOwner = "CORP\DEVAdministrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Web Variables-----------------
$webOwner = "CORP\DEVAdministrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
}
"qa"
{
#--------------Web App Variables-----------------
$webApplicationName = "QAIntranet"
$appPoolUserName = "CORP\QAAdministrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$appPoolPassword = ConvertTo-securestring "pass@word1" -asplaintext -force ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$portNumber = "80" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$serverName = "QASPSERVER" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Site Collection Variables-----------------
$siteName = "QAIntranet"
$siteManagedPath = "/sites/Intranet" #"" if root level. Otherwise, something like "/sites/PortalName"
$siteUrl = "http://" + $serverName + ":" + $portNumber + $siteManagedPath
$siteOwner = "CORP\QAAdministrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Web Variables-----------------
$webOwner = "CORP\QAAdministrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
}
"prod"
{
#--------------Web App Variables-----------------
$webApplicationName = "Intranet"
$appPoolUserName = "CORP\Administrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$appPoolPassword = ConvertTo-securestring "pass@word1" -asplaintext -force ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$portNumber = "80" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
$serverName = "SPSERVER" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Site Collection Variables-----------------
$siteName = "Intranet"
$siteManagedPath = "/sites/Intranet" #"" if root level. Otherwise, something like "/sites/PortalName"
$siteUrl = "http://" + $serverName + ":" + $portNumber + $siteManagedPath
$siteOwner = "CORP\Administrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
#--------------Web Variables-----------------
$webOwner = "CORP\Administrator" ##########GLOBAL VARIABLE (UPDATE IF NECESSARY)
}
}
#--------------CREATE WEB APP AND SITE COLLECTION-----------------
if($createWebApplication)
{
CreateWebApplication $webApplicationName $appPoolUserName $appPoolPassword $portNumber $serverName $timeZoneId
}
if($createSiteCollection)
{
CreateSiteCollection $siteName $siteUrl $siteOwner $siteTemplate $removeLibraryApprovalWorkflow $disableCheckoutModeration $addTeamCollaborationFeature
}
UpdateMasterPageUrl $siteUrl "" $masterPageUrl $false
#--------------ADD SITE VISIBILITY SITE COLUMN-----------------
CreateSiteColumnMultiChoice $siteUrl $siteColumnName $listSiteVisibilityChoices $siteColumnDefaultValue $siteColumnGroup $true
#--------------CREATE SUB SITES-----------------
for($i=0; $i -lt $webTitles.Length; $i++)
{
$webUrl = ($siteUrl + $webUrls[$i])
CreateWeb $siteUrl $webUrls[$i] $siteManagedPath $webTitles[$i] $webOwner $webTemplate $removeLibraryApprovalWorkflow $addTeamCollaborationFeature
UpdateMasterPageUrl $siteUrl ($siteManagedPath + $webUrls[$i]) $masterPageUrl $false
}
#--------------ADD SITE GROUPS-----------------
for($i=0; $i -lt $siteGroups.Length; $i++)
{
AddSiteGroup $siteUrl $siteGroups[$i] $siteGroupDefaultUsers[$i] $siteGroupOwners[$i] ""
}
#--------------ADD PRIVATE WEB PAGES AND LISTS / SET PERMISSIONS-----------------
for($i=0; $i -lt $privateTeamUrls.Length; $i++)
{
$webUrl = ($siteUrl + $privateTeamUrls[$i])
for($j=0; $j -lt $publishingPages.Length; $j++)
{
CreatePublishingPage $siteUrl $privateTeamUrls[$i] $publishingPages[$j] $publishingPages[$j].Replace(".aspx", "")
}
for($j=0; $j -lt $listNames.Length; $j++)
{
CreateList $siteUrl $privateTeamUrls[$i] $listNames[$j] "" $listNameTemplates[$j]
AddListSiteColumn $siteUrl $privateTeamUrls[$i] $listNames[$j] "Site Visibility"
}
SetRoleInheritance $siteUrl $privateTeamUrls[$i] $true
}
#--------------SET PRIVATE WEB GROUP PERMISSIONS-----------------
for($i=0; $i -lt $privateTeamUrls.Length; $i++)
{
RemoveGroupPermission $siteUrl $privateTeamUrls[$i] ($siteName + " Visitors")
AddGroupPermission $siteUrl $privateTeamUrls[$i] ($permissionPermissionNames[$i] + " Visitors") "Read"
AddGroupPermission $siteUrl $privateTeamUrls[$i] ($permissionPermissionNames[$i] + " Members") "Contribute"
AddGroupPermission $siteUrl $privateTeamUrls[$i] ($permissionPermissionNames[$i] + " Owners") "Full Control"
}
#--------------CREATE LIST FOLDERS, PERMISSIONS, AND VIEWS-----------------
for($i=0; $i -lt $privateTeamUrls.Length; $i++)
{
for($l=0; $l -lt $listNames.Length; $l++)
{
AddListFolder $siteUrl $privateTeamUrls[$i] $listNames[$l] $listFolderName $true
SetListFolderPermissions $siteUrl $privateTeamUrls[$i] $listNames[$l] $listFolderName $listFolderPermissionGroup $listFolderPermission
for($m=0; $m -lt $listSiteVisibilityChoices.Length; $m++)
{
AddListView $siteUrl $privateTeamUrls[$i] $listNames[$l] $listSiteVisibilityChoices[$m] $fieldNames $privateCamlQuery 10 $false $false $false
}
#ADD Content Query Web Part to Public Page
AddContentByQueryWebPart $siteUrl $publicTeamUrls[$i] $privateTeamRelativeUrls[$i] $privateTeamUrls[$i] ($publicTeamUrls[$i] + $pageUrl) $listNames[$l] $listNames[$l] $listViewColumnFilterName $listViewColumnPublicFilterValue $listViewColumnPublicFilterValue $listViewItemLimit $listViewZoneId 0
#ADD Content Query Web Part to Intranet Home Page #TODO: FIGURE OUT WHY THIS HAS TO BE RUN SEPARATELY
#AddContentByQueryWebPart $siteUrl "" $privateTeamRelativeUrls[$i] $privateTeamUrls[$i] ($siteUrl + $pageUrl) $listNames[$l] ($privateTeamNames[$i]+" - "+$listNames[$l]) $listViewColumnFilterName $listViewColumnIntranetFilterValue $listViewColumnIntranetFilterValue $listViewItemLimit $listViewZoneId 0
}
}
#--------------ADDING SAMPLE DATA TO LISTS-----------------
for($i=0; $i -lt $privateTeamUrls.Length; $i++)
{
AddSampleAnnouncements $siteUrl $privateTeamUrls[$i] $privateTeamNames[$i] "Announcements"
AddSampleAnnouncements $siteUrl $privateTeamUrls[$i] $privateTeamNames[$i] "News"
AddSampleCalendarEvents $siteUrl $privateTeamUrls[$i] $privateTeamNames[$i] "Team Calendar"
AddSampleLinks $siteUrl $privateTeamUrls[$i] $privateTeamNames[$i] "I Want To"
}
Write-Host "--------------FINISHED CREATING PUBLISHING PORTAL-----------------"
}
#--------------START HERE---------------------------------------------------------------------------------------------------
Clear-Host
$env = "dev" #"dev", "qa", or "prod" Make sure to update variables within the function.
$createWebApplication = $false
$createSiteCollection = $true
CreateSamplePublishingPortal $createWebApplication $createSiteCollection $env
[System.Console]::Read()