EldoS | Feel safer!

Software components for data protection, secure storage and transfer

EnumerateDirectory - SharePoint

Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.
#32684
Posted: 03/20/2015 17:57:52
by Tim Sullivan (Standard support level)
Joined: 05/01/2008
Posts: 15

Hi,
I'm currently working on creating a virtual directory to show SharePoint documents. I've been reading the articles and reviewing the sample code you provide. I'm having trouble with the directory enumeration code. If you have any suggestions for working with SharePoint, in general, please feel free to share. My best idea thus far is modeling the approach from the Mapper example.

Here is my current code:
Code
        private static void CbFsEnumerateDirectory(CallbackFileSystem Sender,
            CbFsFileInfo DirectoryInfo,
            CbFsHandleInfo HandleInfo,
            CbFsDirectoryEnumerationInfo DirectoryEnumerationInfo,
            string Mask,
            int Index,
            bool Restart,
            ref bool FileFound,
            ref string FileName,
            ref string ShortFileName,
            ref DateTime CreationTime,
            ref DateTime LastAccessTime,
            ref DateTime LastWriteTime,
            ref long EndOfFile,
            ref long AllocationSize,
            ref CBFS_LARGE_INTEGER FileId,
            ref ACCESS_MASK FileAttributes)
        {
            if (DirectoryEnumerationInfo.UserContext == IntPtr.Zero)
            {
                //initial enumeration check
                ClientContext clientContext = new ClientContext("http://MySPServer");
                List docList = clientContext.Web.Lists.GetByTitle("Documents");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/>" +
                                    @"<Value Type='Text'>" + @"*" + @"</Value>" +
                                    @"</Eq></Where><RowLimit>100</RowLimit></Query></View>";

                ListItemCollection listItems = listItems = docList.GetItems(camlQuery);
                clientContext.Load(docList);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();

                GCHandle gch = GCHandle.Alloc(listItems);
                DirectoryEnumerationInfo.UserContext = GCHandle.ToIntPtr(gch);
            }
            else
            {
                //continue existing enumeration
            }
        }
#32685
Posted: 03/21/2015 02:09:10
by Eugene Mayevski (EldoS Corp.)

1. If Restart is true, it is reasonable to retrieve the list from the server again. I.e. the first code piece can be modified like shown below.

2. Once the UserContext is initializes (both on the first call to OnEnumerateDirectory and on consequent ones), you take the information from the list and set the "ref" parameters of the method accordingly. The index of item to take from the list is defined by Index parameter.

Code
if ((DirectoryEnumerationInfo.UserContext == IntPtr.Zero) || Restart)
{
  ...
  clientContext.ExecuteQuery();
  if (Restart && (DirectoryEnumerationInfo.UserContext != IntPtr.Zero))
  {
    // release the GCHandle contained in UserContext
    GCHandle.FromIntPtr(DirectoryEnumerationInfo.UserContext).Free();
    DirectoryEnumerationInfo.UserContext = null;
  }
  GCHandle gch = GCHandle.Alloc(listItems);
  DirectoryEnumerationInfo.UserContext = GCHandle.ToIntPtr(gch);
}

if (DirectoryEnumerationInfo.UserContext != IntPtr.Zero)
{
  //return information from the enumeration
  if (Index < listItems.Count)
  {
    FileFound = true;
    FileName = listItems[Index].SomeFileNameField;
    ...
  }
  else
  {
    FileFound = false;
    // release the GCHandle contained in UserContext
    GCHandle.FromIntPtr(DirectoryEnumerationInfo.UserContext).Free();
    DirectoryEnumerationInfo.UserContext = null;
  }
}


Please let us know how this works for you.


Sincerely yours
Eugene Mayevski

Reply

Statistics

Topic viewed 1437 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!