Jul 24

Here's the code to download files of a certain type (in the example, I set it to find documents in the list with the .doc extension) from a document library to your hard drive.

    SPWeb mySite = SPContext.Current.Web;
    mySite.AllowUnsafeUpdates = true;
    SPList oList = mySite.Lists[<ListName>];
    SPQuery oQuery = new SPQuery();
    oQuery.Query = "<Where><Eq><FieldRef Name='DocIcon'/><Value Type='Computed'>doc</Value></Eq></Where>";
    SPListItemCollection listItems = oList.GetItems(oQuery);

    for (int i = 0; i < listItems.Count; i++) {
            SPListItem item = listItems[i];

            // copy this file to the temp folder
            byte[] binfile = item.File.OpenBinary();

            FileStream fs = new FileStream("C:\\Temp\\" + item["Name"], FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(binfile);
            bw.Close();
    }

May 1
Sorted List with multiple DateTime keys
Posted by Wei in C# on 05 1st, 2009| | No Comments »

What I needed to do was to sort a list of dates and corresponding names in order of date. Problem was that sometimes the dates duplicate and as a result, a standard SortedList would crash (since keys need to be unique). Anyway, here's a workaround to that problem. We create a new class for the project as follows:

    public class ComparableDateTime : IComparable
    {
        private DateTime mDT;

        public int CompareTo(object obj)
        {
            if (mDT.Ticks < ((ComparableDateTime)obj).mDT.Ticks) return -1;
            return 1;
        }

        public ComparableDateTime(DateTime myDate)
        {
            mDT = myDate;
        }
    }

Then once we have the class, we can declare a SortedList with that class:

using System.Collections.Generic;

SortedList<ComparableDateTime, string> mySortedList = new SortedList<ComparableDateTime,string>();

DateTime someDate = DateTime.Today;

mySortedList.Add(new ComparableDateTime(someDate,"someString");
mySortedList.Add(new ComparableDateTime(someDate,"someString2");

for (int i = 0; i < mySortedList.Count; i++)
{
    writer.Write(mySortedList.Values[i].ToString());
}

Apr 20

Here's a little function that I wrote to return you the Monday's date for this week, next week, etc...

GetNextWeekDates(DayOfWeek.Monday, 0);

Change DayOfWeek.Monday to any day of the week and it will return you the next date on that particular day. The integer represents which date you want to return. 0 means that you want this week's Monday. Set it to 1, then it's the coming Monday. Set it to 2 and you get the date of the Monday after.

        private DateTime GetNextWeekDates(DayOfWeek targetDay, int weekNumber)
        {
            int daysDifference = (int)targetDay - (int)DateTime.Today.DayOfWeek;

            if (daysDifference == 0) weekNumber++;

            DateTime returnDate;

            if (daysDifference < 0)
            {
                returnDate = DateTime.Today.AddDays(daysDifference + 7 + ((weekNumber - 1) * 7));
            }
            else
            {
                returnDate = DateTime.Today.AddDays(daysDifference + ((weekNumber - 1) * 7));
            }

            return returnDate;
        }

Apr 3

First thing to clear up is this... a document library is not a folder and a folder is not a document library. A folder resides in a document library and the SPFolder class doesn't have the same methods as the SPDocumentLibrary class (unfortunately).

For example, you can get the last modified date of any file within an entire document library, but you can't get the last modified date of any file within a folder within that document library. I found this frustrating because that was exactly what I had to do.

Anyway, when you browse to a document library and click on a folder within that document library, you get a huge URL. One that looks something like http://intranet/site/subsite/DocLib/Forms/AllItems.aspx?RootFolder=%2fsite%2fsubsite%2fDocLib%2fFolderName&blah blah blah. Copy that link into a list with a URL field and sometimes you see this... http://intranet/site/subsite/DocLib/FolderName. Now, that's annoying when trying to figure out what the folder is in the URL that the user pastes.

So firstly, what you need to do is check if there is a value for RootFolder in the URL and if there is, convert it into the shortened version so you can more easily manipulate it.

    if (strURL.Contains("RootFolder="))
    {
        // need to shorten it
        int equalMark = strURL.IndexOf("=");
        int ampersandMark = strURL.IndexOf("&");
        string folderURL = SPEncode.UrlDecodeAsUrl(strURL.Substring((equalMark + 1), (ampersandMark - equalMark - 1)));

        SPSite site = new SPSite(strURL);
        strURL = site.Url + folderURL; // now the url is the link direct to the folder itself.
    }

From this smaller URL, you can get the document library details (see here). Then as far as I know, you have to loop through all the folders in the document library to find the one you are after.

    SPSite site = new SPSite(strURL);
    SPWeb web = site.OpenWeb();

    string[] explode = strURL.Replace(web.Url, "").Split(new char[] { '/' });
    string splitURL = explode[1];
    string splitFolderName = explode[2];

    string docLibName = SPEncode.UrlDecodeAsUrl(splitURL); // (this is also the list name with the location we're after)

    foreach (SPListItem folderListItem in web.Lists[docLibName].Folders)
    {
        if (folderListItem.Name == strFolderName) // compare
        {
            // then now we go through the items in this folder and see if there is anything new
            foreach (SPFile file in folderListItem.Folder.Files)
            {
                // you can now go through each file in that folder
            }
        }
    }

Note that web.Lists[docLibName].Folders is of type SPFolder already so you can manipulate the folder if you need to.

Apr 3

Here's what was happening. I needed to extract the document library name from an intranet URL that a user enters. So they would enter something like this as the URL: http://intranet/site/subsite/DocLib1/Forms/AllItems.aspx and from there I need to extract DocLib1 as SPDocumentLibrary. Problem here is that no matter the depth, I still need to find the document library name and manipulate it as SPDocumentLibrary.

I'm not sure if my way is the best way to do it, but it did work...

            SPSite site = new SPSite(strURL);
            SPWeb web = site.OpenWeb();

            string[] explode = strURL.Replace(web.Url, "").Split(new char[] { '/' });
            string splitURL = explode[1];

            string docLibName = SPEncode.UrlDecodeAsUrl(splitURL); // (this is also the list name with the location we're after)
            SPList thisDocLib = web.Lists[docLibName];

If you do a check for thisDocLib.BaseType, you will find it's of the type DocumentLibrary.

Now, how would it work if it were the folder that we were after instead of a document library?

« Previous Entries Next Entries »