How to pass a controls property as a Command Parameter in WPF

By walter.munoz on November 24th, 2011

A Quick tip on how to send a Controls Property as the Paramter when binding to a command.

5
6
7
8
<Button x:Name="myButton" Command="{Binding SomeCommand}" 
CommandParameter="{Binding ElementName=myButton, Path=PropertyName}">
Click Me
</Button>

SharePoint 2010 How to Change the Search Settings In a Site Collection using PowerShell

By walter.munoz on August 19th, 2011

The following script sets the Site Collection Search Center, Stie Collection Dropdown Mode and Site Collection Search Results Page for all Site Collections that are not a Search Center in the Web Application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#Search Center URL
$searchURL = "/searchcenter/Pages/results.aspx"
#Get the Web Application
$webApplication = Get-SPWebApplication "<a href="http://alvsptw03:11682&quot;">http://localhost"</a>
 
#loop through the sites in the web application
foreach ($site in $webApplication.Sites)
{
      #Get the root web
      $web = $site.RootWeb
      #We want to update all sites that are not a search center
      if ($web.WebTemplate -ne "SRCHCEN")
      {
         #Site Collection Search Center
         $web.AllProperties["SRCH_ENH_FTR_URL"] = $searchURL
 
         #Site Collection Search Dropdown Mode
         #Show scopes Dropdown = ShowDD
         $web.AllProperties["SRCH_SITE_DROPDOWN_MODE"] = "ShowDD"
 
         #Site Collection Search Results Page
         $web.AllProperties["SRCH_TRAGET_RESULTS_PAGE"] = $searchURL
 
         $web.Update()
 
         Write-Host "Updated Search Settings on Site: " $web.Url
 
      }
}

The SRCH_ENH_FTR_URL is the URL or relative path of the Search Center you want to use.

The Drop Down can be set to one of the following values:

Site Collection Search Dropdown Mode

Value

Do you need to set the Sit Collection Search Results Page?

Do Not Show Scopes Dropdown, and default to contextual scope

HideScopeDD_DefaultContextual

YES

Do Not Show Scopes Dropdown, and default to target results page

HideScopeDD

NO

Show scopes Dropdown

ShowDD

YES

Show, and default to ‘s’ URL parameter

ShowDD_DefaultURL

YES

Show and default to contextual scope

ShowDD_DefaultContextual

YES

Show, do not include contextual scopes

ShowDD_NoContextual

NO

Show, do not include contextual scopes, and default to ‘s’ URL parameter

ShowDD_NoContextual_DefaultURL

NO

How To Stop The Timer Job Service Through a PowerShell Script

By walter.munoz on August 17th, 2011

The following PowerShell script loops through an Array of Server Names, stops the SharePoint Timer Job Service, Deletes the files in the folders located in the ProgramData\Microsoft\SharePoint\Config and restarts the SharePoint Timer Job Service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$servers = "ServerName1", "ServerName2"
$deletePath = "C$\ProgramData\Microsoft\SharePoint\Config"
 
#loop through each server
foreach ($server in $servers)
{
     #Stop the service
     #Get the service for the remote server and stop the service
     Get-WmiObject -computer $server Win32_Service -filter "Name='SPTimerV4'" | Stop-Service
     Write-Host The SPTimerV4 service has been stopped
 
     #setup the server delete path
     $path = "\\$server\$deletePath"
 
     #Get the directories
     $folders = [System.IO.Directory]::GetDirectories($path)
 
    foreach($folder in $folders)
    {
        #remove all the files in the folder if you want to delete the folder remove the \*
        Remove-Item $folder\* -Recurse -Force
    }
 
    #Start the service
    #Get the service for the remote server and stop the service
    Get-WmiObject -computer $server Win32_Service -filter "Name='SPTimerV4'" | Start-Service
    Write-Host The SPTimerV4 service has been started
}

Old Code Keeps Running in a SharePoint Timer Job

By walter.munoz on August 16th, 2011

Keep forgetting this one.

When developing a Custom Timer Job you may and encounter situations where old code keeps getting executed even after you retract / delete / add / deploy the solution. If this is the case then the your old code is probably getting cached in the Timer Job Process. To clear the cache, open up the Windows Task Manager and end the OWTIMER.EXE process:


Another way of clearing the cache is to restart the SharePoint Timer Job Services:

How To Change the ‘From’ Address in a SharePoint Workflow

By walter.munoz on August 14th, 2011

I had a requirement to send a an email on behalf of a user in a workflow so I added a ‘SendEmail’ Activity and configured the ‘From’ property and deployed the workflow.

From Setting:


I tested the workflow and it sent the email from the address configured in the Outgoing E-Mail Settings in Central Administrator. Turns out you can’t send on behalf of using the ‘From’ address using the ‘SendEmail’ Activity.

In order to get around this limitation I added the ‘CodeActivity’, sent a System.Net.Mail.MailMessage and it worked fine.

Example of how to use the System.Net.Mail.MailMessage in a code activity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  private void CodeToSendEmail(object sender, EventArgs e)
  {
       //using System.Net.Mail
       MailMessage message = new MailMessage();
 
       //fill out the mail message
       message.From = new MailAddress("From@company.com");
       message.To.Add("To@company.com");
       message.CC.Add("CC@company.com");
       message.Bcc.Add("BCC@company.com");
       message.Subject = "Subject";
       message.Body = "Hello";
       message.IsBodyHtml = true;
 
       //get the outbound mail address
       string outboundAddress = workflowProperties.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
 
       //Create an SMTP Client Address
       SmtpClient smtpClient = new System.Net.Mail.SmtpClient(outboundAddress);
 
       //Send the address
       smtpClient.Send(message);
   }

How To Remove The Gloss From Apple Touch Icons

By walter.munoz on August 14th, 2011

In order to use an Apple Touch Icon in your site add the following to the header in your site’s html:

?View Code HTML4STRICT
<link rel="apple-touch-icon" href="iconlocation.png">

If you don’t feel your icon needs the default iphone gloss then use the following

?View Code HTML4STRICT
<link rel="apple-touch-icon-precomposed" href="iconlocation.png">

How To Add The Default Groups to a SharePoint Site Using C#

By walter.munoz on August 13th, 2011

The CreateDefaultAssociatedGroups method in SPWeb adds the Owners,
Contributors and Readers group to a SharePoint site.  Description and example below.

Description
The CreateDefaultAssociatedGroups method creates the Ownerd, Contributors and Readers groups to a Web and takes in the following
parameters:

UserLogin: Login Name of the user  you want to add to the Owners Group
UserLogin2: Login Name of the user  you want to add to the Owners Group
groupNameSeed: The prefix of the groups (example: If you set this as MyGroups then the resulting groups will be “MyGroups Owners”) The best practice is to keep the groupNameSeed as the site title.

Example

1
2
3
4
5
6
using (SPWeb web = newSite.OpenWeb())
{
     //creates default SharPoint Groups
     web.CreateDefaultAssociatedGroups("domain//username1", "domain//username2", web.Title);
     web.Update();
}

How to Add Users to the Site Collection Administrators

By walter.munoz on August 13th, 2011

This one gave me a hard time. I was trying to add users to the Site Collection Administrators for a site;

This button:

I thought ok easy and tried the following, which executed without any errors:

?View Code CSHARP
1
2
3
4
5
6
7
8
using (SPSite site = new SPSite("http://url/sites/test"))
{
    using (SPWeb web = site.RootWeb)
    {
        web.SiteAdministrators.Add("Domain\\newAdmin", "newAdmin@company.com", "New Admin", "");
        web.Update();
     }
}

I go back to the site and the user was not added. I don’t know why this does not work but there is a pretty easy workaround and here it is:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
using (SPSite s = new SPSite("http://url/sites/test"))
{
      using (SPWeb web = s.RootWeb)
      {
            web.AllUsers.Add("Domain\\newAdmin", "newAdmin@company.com", "New Admin", "");
            SPUser user = web.AllUsers["Domain\\newAdmin"];
            user.IsSiteAdmin = true;
            user.Update();
}

}