Archive for the ‘2010’ Category

I recently needed to move a MOSS 2007 wiki into a MOSS 2010 wiki. There’s no easy OOTB way to do this and third party tools are expensive, so I am rolling my own apps to handle an export of wiki content from MOSS 2007 and another app to do the import to MOSS 2010.

This application –

  • Filters a directory of files based on a provided extension
  • Parses the content of each file
  • Creates a new wiki document with the same name as the file
  • Inserts the parsed content into the new wiki file.

Refer to this article to exporting a wiki from SharePoint 2007
https://jeremybranham.wordpress.com/2010/12/20/export-a-sharepoint-2007-wiki-to-the-filesystem/

I used these 2 articles as a reference to build this app.
http://www.zimmergren.net/archive/2009/11/30/sp-2010-getting-started-with-the-client-object-model-in-sharepoint-2010.aspx
http://msdn.microsoft.com/en-us/library/ee857094.aspx

First, you need the correct client DLLs to access the SharePoint Client Object Model. If you don’t have MOSS 2010 installed on your development machine, you can download the correct files here.
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b4579045-b183-4ed4-bf61-dc2f0deabe47

After you’ve installed the client Object Model Libraries, make a reference to them in your project and you will be on your way to building a client app.

I ran into a couple of ‘gotchas’ while puttting this one together. Here is a segment of code that gave me some trouble.


using (SP.ClientContext ctx = new SP.ClientContext(txtURL.Text))
{
//* Add page to library
SP.Web web = ctx.Web;
var list = web.Lists.GetByTitle(cmbLists.SelectedItem.ToString());
ctx.Load(list);
SP.Folder rootFolder = list.RootFolder;
string serverRelativeUrl = web.ServerRelativeUrl + "/" + list.Title + "/" + "AnotherTestFromOM" + ".aspx";
SP.File wikiPage = rootFolder.Files.AddTemplateFile(serverRelativeUrl, SP.TemplateFileType.WikiPage);
SP.ListItem wikiItem = wikiPage.ListItemAllFields;
wikiItem["WikiField"] = "<p>This is a test</p>";
ctx.ExecuteQuery();
}
  • web.ServerRelativeUrl = ‘web.ServerRelativeUrl’ threw an exception of type ‘Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException’
    • I found that a call to ExecuteQuery() is required before access to certain properties are available.
  • Column ‘WikiField’ does not exist
    • For some reason the WikiField didnt exist in the ‘Pages’ enterprise wiki library but it worked in another wiki library that I created.

 

Here is the corrected segment –
     

        private void CreateNewWikiDoc(string fileName, string content)
        {
            using (SP.ClientContext ctx = new SP.ClientContext(txtURL.Text))
            {
                SP.Web web = ctx.Web;
                var list = web.Lists.GetByTitle(cmbLists.SelectedValue.ToString());
                SP.Folder rootFolder = list.RootFolder;
                ctx.Load(list);
                ctx.Load(rootFolder);
                ctx.ExecuteQuery();
                string serverRelativeUrl = rootFolder.ServerRelativeUrl + "/" + fileName + ".aspx";
                SP.File wikiPage = rootFolder.Files.AddTemplateFile(serverRelativeUrl, SP.TemplateFileType.WikiPage);
                SP.ListItem wikiItem = wikiPage.ListItemAllFields;
                wikiItem["WikiField"] = content;
                wikiItem.Update();
                ctx.ExecuteQuery();
            }
        }

 

Here is the complete backend. When I can clean this up a bit, I’ll try to put something in codeplex if there’s not already something there like it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SharePoint;
using SP = Microsoft.SharePoint.Client;
using System.IO;

namespace MOSS2010_Wiki_Import
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();

            // Show open file dialog box
            dlg.ShowDialog();

            // Process open file dialog box results
            if (!string.IsNullOrEmpty(dlg.SelectedPath))
            {
                txtSource.Text = dlg.SelectedPath;
            }
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            if (!ParametersValidated())
                return;
            try
            {
                ParseFiles();
            }
            catch (Exception ex)
            {
                txtOutput.Text = ex.Message;
            }
        }

        void ParseFiles()
        {

            // Put all files that match the selected extension in root directory into array.
            string[] fileArray = Directory.GetFiles(txtSource.Text, "*." + cmbExt.Text); // <-- Case-insensitive

            //Parse the file list
            foreach (string fileNameAndPath in fileArray)
            {
                // Get the contents of the file
                string content = GetFileContent(fileNameAndPath);

                // Call the method that creates the new file.
                CreateNewWikiDoc(RemoveExtAndPath(fileNameAndPath), content);
            }
        }

        private string RemoveExtAndPath(string fileNameAndPath)
        {
            string[] pathSegments;
            pathSegments = fileNameAndPath.Split('\\');
            return pathSegments.Last().Replace("." + cmbExt.Text, "");
        }

        private string GetFileContent(string fileName)
        {
            TextReader tr = new StreamReader(fileName);
            return tr.ReadToEnd();
        }

        private void CreateNewWikiDoc(string fileName, string content)
        {
            using (SP.ClientContext ctx = new SP.ClientContext(txtURL.Text))
            {
                SP.Web web = ctx.Web;
                var list = web.Lists.GetByTitle(cmbLists.SelectedValue.ToString());
                SP.Folder rootFolder = list.RootFolder;
                ctx.Load(list);
                ctx.Load(rootFolder);
                ctx.ExecuteQuery();
                string serverRelativeUrl = rootFolder.ServerRelativeUrl + "/" + fileName + ".aspx";
                SP.File wikiPage = rootFolder.Files.AddTemplateFile(serverRelativeUrl, SP.TemplateFileType.WikiPage);
                SP.ListItem wikiItem = wikiPage.ListItemAllFields;
                wikiItem["WikiField"] = content;
                wikiItem.Update();
                ctx.ExecuteQuery();
            }
        }

        /// <summary>
        /// Todo: Insert Validation
        /// </summary>
        /// <returns></returns>
        private bool ParametersValidated()
        {
            return true;
        }

        private void btnGetLists_Click(object sender, RoutedEventArgs e)
        {
            cmbLists.Items.Clear();
            using (SP.ClientContext ctx = new SP.ClientContext(txtURL.Text))
            {
                var web = ctx.Web;

                ctx.Load(web);
                ctx.Load(web.Lists);

                ctx.ExecuteQuery();

                foreach (SP.List list in web.Lists)
                {
                    if (list.Hidden)
                        continue;
                    if (list.BaseType == SP.BaseType.DocumentLibrary)
                    cmbLists.Items.Add(list.Title);
                }
            } 
        }
    }
}