Thursday, November 29, 2007

How to: Create Search Scopes in MOSS 2007 Enterprise Search

Patrick Tisseghem made some nice video tutorials about Enterprise Search. Watch and learn how to create and expose search scopes in Microsoft Office SharePoint Server 2007 Enterprise Search.

If you're looking for some more video tutorials about enterprise search... surf to Msn and use the search engine.

Wednesday, November 21, 2007

Coding against the SharePoint Object Model (Some useful code snippets)

I thought it might be handy to write down some useful code snippets. These snippets are often used when you code against the SharePoint Object Model...

Delete all items in a document library:

using (SPSite sitecollection = new SPSite("http://..."))
using (SPWeb web = sitecollection.OpenWeb())
SPList myList = web.Lists["Document library"];
int amountOfFiles = myList.Items.Count;

for (int i = amountOfFiles - 1; i >= 0; i--)
//Delete Document Library item
web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = false;

Uploading a file to a document library:

using (SPSite sitecollection = new SPSite("http://..."))
using (SPWeb web = sitecollection.OpenWeb())
SPFolder docFolder = web.GetFolder("document library");

FileStream fStream = File.OpenRead(urlOfDocumentOnServer);
Byte[] ContentsContents = new byte[Convert.ToInt32(fStream.Length)];
fStream.Read(Contents, 0, Convert.ToInt32(fStream.Length)); }

web.AllowUnsafeUpdates = true;
docFolder.Files.Add(filename, Contents);
web.AllowUnsafeUpdates = false;

Add a Contenttype to a ListItem:

using (SPSite sitecollection = new SPSite("http://..."))
using (SPWeb web = sitecollection.OpenWeb())
SPList list = web.Lists["document library"];

SPContentTypeCollection ctcollection = list.ContentTypes;
SPContentType contenttype = ctcollection[itemcontenttype];

SPListItem item = list.Items[0];
item["ContentTypeId"] = contenttype.Id;

web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = false;

Add an Eventhandler on a SharePoint list:
SPList list = web.Lists["List"];
SPEventReceiverType type = SPEventReceiverType.ItemAdded;
string assembly = "";
string className = "";
list.EventReceivers.Add(type, assembly, className);


Delete an EventHandler from a SharePoint list:
Guid guid = ...;


Start a workflow on an item
Guid wfBaseId = new Guid("{6BE0ED92-BB12-4F8F-9687-E12DC927E4AD}");
SPSite site = ...;
SPWeb web = site.OpenWeb();
SPList list = web.Lists["listname"];
SPListItem item = list.Items[0];
SPWorkflowAssociation associationTemplate= list.WorkflowAssociations.GetAssociationByBaseID(wfBaseId);
site.WorkflowManager.StartWorkflow(item, associationTemplate, "");

Note: the workflow base ID can be found in the workflow.xml in the feature folder
Source: Steven Van de Craen


Other SharePoint Code Snippets...
Checking if a workflow is running on an item/SPWorkflowState
Detect an item published in ItemUpdating eventReceiver
How (not) to complete workflow tasks using code in WSS3
Modifying SharePoint Workflow Task Programmatically Problem
How to create MySite Programmatically
Add users of an AD Group to the SharePoint Site

Monday, November 19, 2007

Using Custom XML islands together with Content Controls

To realize a link between Word 2007 Content Controls and Custom XML you can either do it with a tool such as Word 2007 content control toolkit or do it programmatically with XPath and add a dataBinding node for each Structured Document Tag(SDT) like this example:

w:dataBinding w:prefixMappings="" w:xpath="/myXml/dataRow/dataField" w:storeItemID="{CFD5333D-3117-4F96-8B72-CC1D814E0755}"

If you want to fill the custom xml dynamically - try the following:

using (Package p = Package.Open(generatedPath, FileMode.Open, FileAccess.ReadWrite))
// Now we can get the path to our custom XML in the template
Uri partUri = new Uri("/customXml/item1.xml", UriKind.Relative);
PackagePart part = p.GetPart(partUri);
// Overwrite existing part, therefore open with FileMode.Create
using (Stream s = part.GetStream(FileMode.Create))
// Now use the XmlSerialize to write back the content
XmlSerializer serializer = new XmlSerializer(typeof(Employee));
serializer.Serialize(s, employee);

// Flush memory-content back to the package

Just navigate to MzCool if you need an explanation of the whole context...

Thursday, November 15, 2007

Xperimenting with OpenXML

When you start experimenting with WordProcessingML (or Open XML in general), you definitely must take a look at the following tools:

Word 2007 Content Control Toolkit
This light-weight tool can open any word open xml
document and lists all content controls and available XML parts. It also enables mapping between content controls and custom XML. This all by using the System.IO.Packaging library available in .NET 3.0.

Package Explorer
This tool let you look inside a Open XML package, edit XML parts,...
[Thx to Maarten, who recommended me to made use of these]

If you need a lot of information about OpenXML - Download this great book (Open XML: The Markup explained)

Thursday, November 8, 2007

Silverlight MSDN event

On Halloween evening, I attended a MSDN event in Utopolis Mechelen dedicated to Silverlight. If you're curious about the content of the event - Take a look at the presentation slide decks and demo's.