Albert Jan Schot
 

Create a List based on a Template (.stp)

17

Jun

So here you found yourself clicking together a list to let users add items early in your development process, next thing you know the list is too big, and they want it to be deployed on their production servers. Or you want to upload an stp and create a list based on it (in my case; I rather upload and deploy an stp create a list based on that, then create an event receiver that added the items by code).

So I spend some time searching the net and created a FeatureReceiver containing the following code:

   1:  /// <summary>
   2:  /// Uploads site Templates
   3:  /// </summary>
   4:  /// <param name="templateGallery"></param>
   5:  /// <param name="templateFiles"></param>
   6:  private void UploadTemplates(SPDocumentLibrary templateGallery, 
string[] templateFiles)
   7:  {
   8:      if (templateGallery != null)
   9:      {
  10:          foreach (string template in templateFiles)
  11:          {
  12:              System.IO.FileInfo fileInfo = new System.IO.FileInfo
(template);
  13:              SPQuery query = new SPQuery();
  14:              query.Query = string.Format("<Where><Eq><FieldRef 
Name='FileLeafRef'/>" +

15: "<Value Type='Text'>{0}</Value></Eq></Where>",

fileInfo.Name);

  16:              SPListItemCollection existingTemplates = 
templateGallery.GetItems(query);
  17:              int[] Ids = new int[existingTemplates.Count];
  18:              for (int i = 0; i < existingTemplates.Count; i++)
  19:              {
  20:                  Ids[i] = existingTemplates[i].ID;
  21:              }
  22:              for (int j = 0; j < Ids.Length; j++)
  23:              {
  24:                  templateGallery.Items.DeleteItemById(Ids[j]);
  25:              }
  26:              byte[] stp = System.IO.File.ReadAllBytes(template);
  27:              templateGallery.RootFolder.Files.Add(fileInfo.Name, stp);
  28:          }
  29:      }
  30:  }

And call that with a:

   1:   string directory = properties.Definition.RootDirectory;
   2:  if (!directory.EndsWith(@"\"))
   3:                          directory += @"\"; directory += "ListTemplates";
   4:                      if (System.IO.Directory.Exists(directory))
   5:                      {
   6:                          string[] templates = System.IO.Directory.
GetFiles(directory, "*.stp", System.IO.SearchOption.TopDirectoryOnly);
   7:                          SPDocumentLibrary listTemplates = 
thisWeb.GetCatalog(SPListTemplateType.ListTemplateCatalog) as SPDocumentLibrary;
   8:                          UploadTemplates(listTemplates, templates);
   9:                      }
  10:                      else
  11:                      {
  12:                          //log error or empty message
  13:                      }

That small piece of code will upload all .stp files to your ListTemplateGallery allowing you to create Lists based on them with the following code:

   1:  foreach (SPListTemplate template in thisSite.GetCustomListTemplates
(thisWeb))
   2:  {
   3:  if (template.Name == "YourTEmplateName")
   4:      {
   5:          listTemplate = true;
   6:          Guid processGuid = subWeb.Lists.Add("ListName", "ListDescription", 
template);
   7:          SPList processlist = subWeb.Lists[processGuid];
   8:   
   9:          processlist.Update();
  10:          subWeb.Update();
  11:      }
  12:  }

For me it saved me a lot of work, hope it will do the same for you.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Naming office files in a Library

18

Feb

So everyone running MOSS works with libraries, and working with office files stored on libraries. Most of the times it works as a bliss, but there a few notes. Within the naming of folders in your library NEVER use the following characters, since while your SharePoint works nicely with them, your Word or PowerPoint will actually fail trying to open the file, giving  you an error that it cannot be read.

  • $
  • &
  • +
  • ,
  • /
  • :
  • ;
  • =
  • ?
  •  @

Just refrain from using these characters either in your filename, folder name or anything else that goes in the url. And make sure all your users know about this.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Sorting a list on a full month name

12

Feb

Sorting a list on Month by using a calculated field. Ever wanted to sort your view of a list based on Months, still wanting to display January, February. The result of sorting on that column would actually giving you an A, B, C result rather then returning them in chronological order.

Getting that done can be quite easily obtained by using calculated fields, since they allow you to 'as the name says' calculate values based on other values already stored on your columns, and that what we want, though before we start some basic understanding of the calculated fields might come in handy.

A calculated field is always a 'new' value based on existing ones, and can be of the type: string, number, value, date/time or yes/no. You can find some more information about all of the functions on http://office.microsoft.com/en-us/sharepointtechnology/HA011609471033.aspx (make sure you check that one out, since in this post I will only cover a few basics.

Let’s say we have a first name, last name and we want to concatenate a Full Name

=[column1]&" "&[column2] // column1, column2

Saying that instead of writing the full first name you could also 'split' it, and only use the first character

=LEFT([column1]; 1)&". "&[column2] // c. column2

Enough about the basics, you can add a calculated column and use the =TEXT([DateColum], "mm") to get the month value into a text field (01, 02, 03 instead of the Formatted dates). Next thing, just sort your view on that column, and hide it, and show your nicely formatted date (something like January, February) and as you will see its nicely sorted.

 

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot
Page 1 of 1 in the SharePointLists category