Jul 152010
 

I’ve just recently attempted to upload an entire directory up to sharepoint.  I did initially try doing this using WebDAV but the speed was unimaginable slow (30 – 40 secs for a 1KB gif image).  I didn’t want to upload the images through the browser as you can only do 100 at a time (I had ~3,500 images plus ~4,000 documents).

So I looked at copying over the files using the sharepoint object model!  It turns out that this is speedy fast and works a treat!  There’s an MSDN article I used as my basis but I’ve made a few changes so it uploads a whole directory instead of just a single file.  Here’s how you do it:

public void UploadImages()
{
    this.CopyDirectory(@"\\some\server\images", "PublishingImages");
    this.CopyDirectory(@"C:\ImagesFolder\ImagesToUpload", "PublishingImages");
}

private void CopyDirectory(string frm, string to)
{
    var serveraddress = "http://server:80/";
    using (var web = new SPSite(serveraddress + to).OpenWeb())
    {
        if (!web.Exists)
        {
            throw new Exception("Web doesn't exist here");
        }

        var di = new DirectoryInfo(frm);
        var fileInfos = di.GetFiles("*.*", SearchOption.TopDirectoryOnly);
        foreach (var fileInfo in fileInfos)
        {
            this.UploadFile(fileInfo.FullName, web);
        }
    }
}

private void UploadFile(string srcUrl, SPWeb web)
{
    if (!File.Exists(srcUrl))
    {
        throw new ArgumentException(String.Format("{0} does not exist",
            srcUrl), "srcUrl");
    }

    byte[] contents;

    using (FileStream fStream = File.OpenRead(srcUrl))
    {
        contents = new byte[fStream.Length];
        fStream.Read(contents, 0, (int) fStream.Length);
        fStream.Close();
    }

    string filename = srcUrl.Substring(srcUrl.LastIndexOf("\\") + 1);

    if (!IsValidFileName(filename))
    {
        throw new Exception(filename + " is not a valid filename.  Please review and try again.")
    }

    var file = web.Files.Add(filename, contents)
    file.Update();
}

private static bool IsValidFileName(string filename)
{
    if (filename.Contains("..") || filename.EndsWith(".") || filename.StartsWith("."))
    {
        return false;
    }

    if (filename.IndexOfAny(new[] { '\"', '#', '%', '&', '*', ':', '<', '>', '?', '\\', '/', '{', '|', '}', '~' }) > -1)
    {
        return false;
    }

    if (filename.Length > 128)
    {
        return false;
    }

    return true;
}
Jul 102010
 

Let’s say you were coding up program that required a number of base 24, how would you go about it? Think now of how you would do it in your own programming language. No really… think about it and think about the resulting type. You’re returning a string right? Depending on your programming language, you might have gone for the approach of assuming a base 10 input, then using division and the remainder operator to go through a dictionary of strings a,b,c,A,B,C. The type of the result is a string which is cumbersome to work with. For example, how would you add 1 to it? I’m guessing you’d have to convert it back to base 10, add one, then reconvert. In my opinion that is a lot of (too much) hassle.

Well how about we think a little differently… Continue reading »

Mar 302010
 

Imagine you are creating a page that you want in place of the default.aspx homepage that resides at the heart and soul of every Site in SharePoint.  Here’s how you do it:

//pageLayout = a page layout
//pubWeb = PublishingWeb

PublishingPage publishingPage = pubWeb.AddPublishingPage("http://sharepointtest/justanothersite/Pages/new-default.aspx", pageLayout);
publishingPage.Update();

pubWeb.DefaultPage = publishingPage.ListItem.File;
pubWeb.Update();

const string checkInComment = "Initial Check In";
publishingPage.CheckIn(checkInComment);
SPFile pageFile = publishingPage.ListItem.File;
pageFile.Publish(checkInComment);
pageFile.Approve(checkInComment);

pageFile.MoveTo(file.ListItemAllFields.ParentList.RootFolder.Url + "/default.aspx", true);
pageFile.Update();
}

The new publishing page is added to the web with the temporary url of “new-default.aspx”. Next we Update() the publishingPage to commit the add.

As we are adding a homepage, we set the DefaultPage property of the publishing web to the listitem file of the publishing page.  Remember here we have to Update() the publishing web or our change will not be reflected.

Check in and approve the file (Only appropriate if you have some kind of approval workflow set up on the site)

As the new publishing page we’re adding is the homepage, we use the MoveTo() method on the file to rename the file to default.aspx while passing the boolean parameter ‘true’ to make sure it overwrites the old ‘default.aspx’ if it is there.