Category Archives: WSS V3

How to upload a list template

public static SPFile UploadListTemplate(string siteUrl, string templatePath)

{

using (SPSite site = new SPSite(siteUrl))

{

SPDocumentLibrary templates = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.
ListTemplateCatalog);

 

System.IO.FileInfo fileInfo = new System.IO.FileInfo(templatePath);

byte[] bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);

 

return templates.RootFolder.Files.Add(fileInfo.Name, bytes);

}

}

How to upload a site template

public static SPFile UploadWebTemplate(string siteUrl, string templatePath)

{

using (SPSite site = new SPSite(siteUrl))

{

SPDocumentLibrary templates = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.WebTemplateCatalog);

 

System.IO.FileInfo fileInfo = new System.IO.FileInfo(templatePath);

byte[] bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);

 

return templates.RootFolder.Files.Add(fileInfo.Name, bytes);

}

}

How to create an item in a discussion list

Creating a discussion in discussion list is different from creating a simple list item. The code for creating a discussion item is:

using (SPSite site = new SPSite(“SITE URL”))

{

using (SPWeb web = site.OpenWeb())

{

SPList list =web.Lists[“LIST TITLE”];

result = SPUtility.CreateNewDiscussion(list.Items, “SUBJECT”);

result[SPBuiltInFieldId.Body] = “BODY”;

result.Update();

}

}

ItemUpdating, AfterProperties and field names

I was checking for a field value change in a WSS V3 list with the help of event handlers with no success.

In the ItemUpdating event, the list item has the current values of the item and the AfterProperties has the values that the item will have after the update

The problem was that I was using the display name of the field rather than the internal name. In the SPListItem you can use the display name but also the internal name, but with the AfterProperties you can't use the display name. You must use the internal name of the field.

How to add a group to a WSS v3 site

Get the group

SPGroup group = web.SiteGroups["GROUP_NAME"];

Create a role assignment

SPRoleAssignment newRoleAssignment = new SPRoleAssignment(group);

Add the role definition (Full Control, Read, Contribute, …) to the role assignment

newRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Full Control"]);

And add the role assignment to the web

web.RoleAssignments.Add(newRoleAssignment);

 

How to use FullTextSqlQuery to search in WSS

First, you have to add the following references:

  • Microsoft.Sharepoint
  • Microsoft.Sharepoint.Search

Second:

DataTable resultsDataTable = new DataTable();

 

using (SPSite site = new SPSite("http://server"))

{

FullTextSqlQuery query = new FullTextSqlQuery(site);

query.QueryText = queryText;

query.ResultTypes = ResultType.RelevantResults;

 

ResultTableCollection resultTables = query.Execute();


if (resultTables.Count > 0)

{

ResultTable relevantResults = resultTables[ResulType.RelevantResults];

resultsDataTable.Load(relevantResults, LoadOption.OverwriteChanges);

}

}

 

return resultsDataTable;

 

In WSS the only ResultType possible is RelevantResults but you have to define it with the following statement:

query.ResultTypes = ResultType.RelevantResults;

 

If you don't do that the ResultTableCollection doesn't return any ResultTable.

You can control the properties returned in the SELECT statement:

string queryText = "SELECT rank, workid, title, path, author, contenttype from Scope()";

 

If you want you can page the results:

int pageSize = 3;

int page = 0;

 


 

query.StartRow = page * pageSize;

query.RowLimit = pageSize;

 

To sort the results you use the ORDER BY statment:

string queryText = "SELECT rank, workid, title, path, author, contenttype from Scope() ORDER BY rank ASC, title DESC";

 

I don't know why but it seems that only the first sort column works. I tried to change the direction and also the property of the second sort column but the results were always ordered the same way.

 

 

How to use KeywordQuery to search in WSS

First, you have to add the following references:

  • Microsoft.Sharepoint
  • Microsoft.Sharepoint.Search

Second:

DataTable resultsDataTable = new DataTable();

 

using (SPSite site = new SPSite("http://server"))

{

 

KeywordQuery query = new KeywordQuery(site);

query.QueryText = queryText;

query.ResultTypes = ResultType.RelevantResults;

 

ResultTableCollection resultTables = query.Execute();


if (resultTables.Count > 0)

{

ResultTable relevantResults = resultTables[ResultType.RelevantResults];

 

resultsDataTable.Load(relevantResults, LoadOption.OverwriteChanges);

}

 

}

 

return resultsDataTable;

 

In WSS the only ResultType possible is RelevantResults but you have to define it with the following statement:

query.ResultTypes = ResultType.RelevantResults;

 

If you don't do that the ResultTableCollection doesn't return any ResultTable.

The properties returned are:

  • Work ID
  • Rank
  • Title
  • Author
  • Size
  • Path
  • Description
  • Write
  • SiteName
  • CollapsingStatus
  • HitHighlightedSummary
  • HitHighlightedProperties
  • ContentClass
  • IsDocument
  • PictureThumbnailURL

If you want you can control exactly what properties are returned by adding properties to the StringCollection – query.SelectProperties

 


 

query.SelectProperties.Add("title");

query.SelectProperties.Add("size");

 

 

In the previous example only the Title and the Size are returned.

If you want you can page the results:

int pageSize = 3;

int page = 0;

 


 

query.StartRow = page * pageSize;

query.RowLimit = pageSize;

 

You can sort the results by adding items to the SortCollection list.


 

query.SortList.Add("Rank", SortDirection.Ascending);

query.SortList.Add("WorkId", SortDirection.Descending);

I don't know why but it seems that only the first sort item works. I tried to change the direction and also the property of the second sort item but the results were always ordered the same way.