Saturday, October 11, 2008

Powershell Script to Add ActiveDirectory (AD) Users

Been a while since I blogged on SharePoint. For the past year I have been more involved in DBA work, and have been more active in my other blog (http://sqlmusings.wordpress.com)

Anyway, here is an old script I have been meaning to post:

#* FileName:  PowerShellTemplate.ps1
#*=============================================================================
#* Created:     [11 December 2007]
#* Author:      Donabel Santos
#* Reqrmnts:   
#* Keywords:   
#*=============================================================================
#* Purpose:    
#*             
#* This Powershell script queries ActiveDirectory for OU-specific
#* users and inserts those users as new records in a Sharepoint
#* out-of-the-box Contact List
#*=============================================================================
 
 
#*=============================================================================
#* SCRIPT BODY
#*=============================================================================
#Powershell Script that queries a specific OU in AD,
#and populates the Contact List
#based on the members that are queried
 
#IMPORTANT NOTE:
#This needs to be run on the MOSS Server because the
#SP Object Model cannot be invoked remotely
 
#Step 1: Install Powershell RC2
#http://support.microsoft.com/kb/925228
 
#Step 2: Set Execution Policy to RemoteSigned (uncomment below)
#Set-ExecutionPolicy RemoteSigned
 
#Step 3: Set the following variables
$siteUrl = "http://moss/sites/test" 
$ou = "LDAP://OU=My Group,OU=Guest Users,DC=domain,DC=ca"
 
#Step 4: Run this script on Powershell on the Sharepoint Server
 
 
 
#Rest of code follows
 
#Load Sharepoint DLL
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
 
#we want to query just the top web of the "test" site collection
$webName = ""  
 
#create a site object
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl) 
 
#the following just displays URL, ID, Name and Users in a table format
$spSite.AllWebs | format-table Url, ID, Name, AllUsers
 
#open web
$spWeb = $spSite.OpenWeb($webName) 
 
#we want to get a handle on contacts
$listName = "Contacts"
 
#get a handle to the Contacts list
$spList = $spWeb.lists[$listName] 
 
  
#Create an AD DirectorySearcher object
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"$ou")
 
#sample filter below, if you need to filter further
#$searcher.filter = "(&(objectClass=user)(givenName=Belle))"
 
#find all that matches
$groups = $searcher.findall()
 
#the following for loop just displays
@(foreach($group in $groups)
{
    [string]$firstname = $group.properties.givenname
    [string]$lastname = $group.properties.sn
    if (($firstname.length -gt 0) -and ($lastname.length -gt 0))
    {
       "Given Name:{0} {1}" -f  $firstname, $firstname.length
    }
}
)
 
#this code block inserts the AD user as a new user 
#into the Sharepoint Contact list
@(foreach($group in $groups)
{
    #extract all properties we need first
    #add fields here if necessary
    [string]$name = $group.properties.name
    [string]$firstname = $group.properties.givenname
    [string]$lastname = $group.properties.sn
    [string]$company = $group.properties.company
    [string]$email = $group.properties.mail
    [string]$objectclass = $group.properties.objectclass
    [string]$distinguishedname = $group.properties.distinguishedname
 
    #now add this to the sharepoint list only if first name 
    #and last name are not empty
 
    #add fields here if necessary
    if (($firstname.length -gt 0) -and ($lastname.length -gt 0))
    {
       $spitem = $spList.Items.Add() 
       $spitem["Last Name"] = $lastname
       $spitem["First Name"] = $firstname
       $spitem["Company"] = $company
       $spitem["E-mail Address"] = $email
       $spitem.Update() 
    }
}
)
 
#voila! we're done!
#*=============================================================================
#* END OF SCRIPT: 
#*=============================================================================

Friday, March 7, 2008

How to Create a Custom Theme

1. Go to your 12 hive
"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\THEMES"

2. Copy one of the theme folders, and rename it.
For example, copy SIMPLE and rename it to DSTheme

3. Inside the DSTheme folder, rename the .INF file to have the same name as your theme.
For example, DSTheme.INF

4. Open DSTheme.INF using a text editor, and rename all instances of "Simple" to "DSTheme"

5. Go to the SPTHEMES.xml file. It is located in
"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\SPTHEMES.XML"

6. Add an entry in the <SPThemes> element:
<Templates>
  <TemplateID>DSTheme</TemplateID>
  <DisplayName>DSTheme</DisplayName>
  <Description>Some Description</Description>
  <Thumbnail>images/thdstheme.gif</Thumbnail>
  <Preview>images/thdstheme.gif</Preview>
</Templates>

In my case, I have put the thumbnail images in the 12 hive images folder:
"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\IMAGES"

This is what's going to show up when anyone previews the theme.

7. Do an iisrest for the server to recognize the new theme.

GOTCHA:
If you make any changes, you are going to have to:
a. do an iisreset again
b. change the theme to a different theme and apply
c. set the theme (again) to your custom theme, and apply

Not very pretty.

Thursday, March 6, 2008

How to Add a Date Filter to a BDC

for Tammie, apologies for the late reply. i hope you will still find this useful.

I've worked with BDCs quite a bit, especially when MOSS was still very new (B2, B2TR, Gold Code, and RC). This is the only way I've gotten Date Filters to work in BDCs.

1. In the <FilterDescriptors> section, add your date filter descriptor.

<FilterDescriptor Type="Comparison" Name="Date Entered">
<Properties>
<Property Name="Comparator" Type="System.String">Equals</Property>
</Properties>
</FilterDescriptor>

2. In the <Parameters> section, add 2 parameters: a low date boundary, and a high date boundary



<Parameter Direction="In" Name="@min_date_entered">
<TypeDescriptor TypeName="System.DateTime" AssociatedFilter="Date Entered" Name="min_date_entered">
<DefaultValues>
<DefaultValue MethodInstanceName="MyListFinder" Type="System.DateTime">2000-01-01 01:01:01Z</DefaultValue>
<DefaultValue MethodInstanceName="MyListSpecificFinder" Type="System.DateTime">2000-01-01 01:01:01Z</DefaultValue>
</DefaultValues>
</TypeDescriptor>
</Parameter>
<Parameter Direction="In" Name="@max_date_entered">
<TypeDescriptor TypeName="System.DateTime" AssociatedFilter="Date Entered" Name="max_date_entered">
<DefaultValues>
<DefaultValue MethodInstanceName="MyListFinder" Type="System.DateTime">3000-01-01 01:01:01Z</DefaultValue>
<DefaultValue MethodInstanceName="MyListSpecificFinder" Type="System.DateTime">3000-01-01 01:01:01Z</DefaultValue>
</DefaultValues>
</TypeDescriptor>

3. For the list of return values, add your datetime column



<Parameter Direction="Return" Name="MyList">
<TypeDescriptor TypeName="System.Data.IDataReader, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" IsCollection="true" Name="MyListDataReader">
<TypeDescriptors>
<TypeDescriptor TypeName="System.Data.IDataRecord, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Name="MyListDataRecord">
<TypeDescriptors>

<TypeDescriptor TypeName="System.String" IdentifierName="my_id" Name="my_id">
<LocalizedDisplayNames>
<LocalizedDisplayName LCID="1033">CA ID</LocalizedDisplayName>
</LocalizedDisplayNames>
</TypeDescriptor>

<TypeDescriptor TypeName="System.DateTime" Name="date_entered">
<LocalizedDisplayNames>
<LocalizedDisplayName LCID="1033">Date Entered</LocalizedDisplayName>
</LocalizedDisplayNames>
<Properties>
<Property Name="DisplayByDefault" Type="System.Boolean">true</Property>
</Properties>
</TypeDescriptor>

</TypeDescriptors>
</TypeDescriptor>
</TypeDescriptors>
</TypeDescriptor>
</Parameter>

Saturday, February 2, 2008

Victoria Code Camp Materials

Sorry I have been sick and out of commission for a week. The past few days, I've been drinking the meds the doctor gave me, which just really knocks me right out.

I apologize for the delay, but here are the materials, finally.


SharePoint 101

SharePoint 101 - Slides


SharePoint Web Parts

SharePoint Web Parts 101 - Slides

SharePoint Web Part Sample Projects:

1. Hello World Web Part

2. Simple Web Part with Custom Properties and Verbs

3. Connectable Web Parts

 

Enjoy! I'll start blogging more again, as soon as I'm 100% better :)

Monday, January 28, 2008

Victoria Code Camp Stuff Coming Really Soon!

The Victoria Code Camp last weekend had been awesome! The location was great, and the attendees were awesome. They were all attentive and curious, and they even laughed at some of my jokes ;)

I have been really sick the past couple of days, but I will be posting the materials/slides and demo code soon. I plan to have it up by tomorrow!

And please feel free to keep the questions coming in :) It makes my work a lot more interesting!

Wednesday, January 16, 2008

How To Change the Duration of the !NEW Tag When Adding Items in SharePoint Lists/Libraries

To disable altogether:

#go to 12 hive
cd /d %programfiles%\Common Files\Microsoft Shared\Web Server Extensions\12\BIN

#to disable !NEW altogether
stsadm.exe -o setproperty -pn days-to-show-new-icon -pv 0 -url [Your Virtual Server's URL]

To extend:


#go to 12 hive
cd /d %programfiles%\Common Files\Microsoft Shared\Web Server Extensions\12\BIN

#to extend showing of the !NEW tag to 4 days, for example
stsadm.exe -o setproperty -pn days-to-show-new-icon -pv 4 -url [Your Virtual Server's URL]


The good news: no IISRESETs needed
The not-so-good news: it affects the behavior of !NEW for every new item/document

Details are documented in the following KB:


How to stop the !New tag from appearing when you add items to your SharePoint Team Services and SharePoint Services Web site
(http://support.microsoft.com/default.aspx?scid=kb;en-us;825510&Product=spts)

Sunday, January 13, 2008

The Case of the Missing Create GUID Tool (guidgen.exe) from Visual Studio 2005

clip_image002

The Create GUID tool is often found under the Tools menu.

image

The actual binary - guidgen.exe - is supposed to be in

C:\Program Files\Microsoft Visual Studio 8\Common7\Tools

Apparently if you did not install Visual C++ when you were installing Visual Studio 2005, you are not going to get guidgen.exe.

I didn’t install Visual C++ in any of my VMs (trying to conserve space, plus I don't use it anyway), which explains why my Create GUID tool is missing.

There might be a better way to do (perhaps browse through the Visual Studio 2005 install files?) but my workaround is to copy guidgen.exe from one of my other Visual Studio 2005 installs.

Saturday, January 12, 2008

Victoria Code Camp, January 26, 2008

I'm doing a couple of sessions at the Victoria Code Camp on January 26, 2008! Thank you to Nolan Zak for giving me this opportunity.

These are my two sessions:

SharePoint 101
This session will provide a high level overview of Windows Sharepoint Services 3.0 and Microsoft Office Sharepoint Server (MOSS 2007). This session will also cover demos of out-of-the-box Sharepoint components like: sites, lists, document libraries, and web parts.

SharePoint Web Parts
This session will cover Web Part concepts, and will walk you through creating custom ASP.NET Web Parts for SharePoint (MOSS 2007, WSS 3.0). Features will also be introduced towards the end of the session.

Schedule is posted at http://www.victoriacodecamp.com/SessionSchedule.aspx.
The list of speakers can be found at http://www.victoriacodecamp.com/CampSpeakers.aspx

Just posting the schedule here as well for your reference:

Morning Schedule
Victoria Code Camp 2008 - AM Schedule


Afternoon Schedule

Victoria Code Camp 2008 - Afternoon Schedule

As usual, I will be posting the materials I will use at the presentations. Hope to see you there! Beautiful Victoria, here I come!

Friday, January 11, 2008

How To Get the SharePoint Default Mail Server/Sender

Couple of methods that will retrieve the default mail server and sender that your Sharepoint web app uses:

//get the server
private string GetSmtpServer()

{
SPWebApplicationCollection spWebApplicationCollection = SPWebService.ContentService.WebApplications;
SPOutboundMailServiceInstance smtpServer = new SPOutboundMailServiceInstance();

if (spWebApplicationCollection != null)
{
foreach (SPWebApplication spWebApplication in spWebApplicationCollection)
{
smtpServer = spWebApplication.OutboundMailServiceInstance;
return smtpServer.Server.Address;
}
}
return string.Empty;
}

 

//get the sender
private string GetDefaultSmtpSender()
{
SPWebApplicationCollection spWebApplicationCollection = SPWebService.ContentService.WebApplications;
string sender = default(System.String);
if (spWebApplicationCollection != null)
{
foreach (SPWebApplication spWebApplication in spWebApplicationCollection)
{
sender = spWebApplication.OutboundMailSenderAddress;
return sender;
}
}
return string.Empty;
}


 

//sample usage
private void SendEmail(string subject, string msg, string to, string cc)
{
MailAddress toAddress = new MailAddress(to, "Main Recipient");
MailAddress fromAddress = new MailAddress(DEFAULT_SENDER);
MailMessage newmail = new MailMessage(fromAddress, toAddress);

//check if we need to cc this to anyone
if (!string.IsNullOrEmpty(cc))
{
MailAddress ccAddress = new MailAddress(cc, "Secondary Recipient");
newmail.CC.Add(ccAddress);
}

newmail.IsBodyHtml = true;
newmail.Subject = subject;
newmail.Body = msg;
System.Net.Mail.SmtpClient mail = new SmtpClient(DEFAULT_MAIL_SERVER);
mail.Send(newmail);
}



By the way, Yaro posted this in this blog too :)

Wednesday, January 9, 2008

How to Register ASP.NET 2.0 in IIS

I was recently building a WSS 3.0 box, and got stumped when I can't see ASP.NET 2.0 listed in IIS. I forgot I needed to register it:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i


That's all, just thought I'd blog this so I don't forget next time :)