dcument library help

Hello everyone, I am trying to get access to documents in a document library and am trying to "server" these documents to an end user. But im having trouble with the basic concepts...

I see code examples like this:

// get the current site
SPWeb site = SPControl.GetContextWeb(Context);

// set the library and view to display
string docLibName = "Shared Documents";
string viewName = "All Documents";

// get the folder
SPFolder docLibFolder = site.Folders[docLibName];

// get library and view
SPDocumentLibrary docLib = (SPDocumentLibrary)site.Lists[docLibName];
SPView docLibView = docLib.Views["View_Name"];

// get all the items in the folder
SPListItemCollection docLibItems = docLib.GetItemsInFolder(docLibView, docLibFolder);

// loop through and display the names of the items
foreach (SPListItem item in docLibItems){
Label1.Text += "Name: " + item["Name"] + "
";
}

And I dont really understand where the "Context" side of things come into it. Can anyone please help me out Im getting complier errors, what is "Context"

Thanks


Answer this question

dcument library help

  • Pascal Mignot

    Thanks for the reply, I will be writing this code using a WCF application which will be on the Sharepoint server.

    Can you please modify the code given or point me in the right direction to make the above code work in a code library project, or if that isnt possible, an ASP.NET 2.0 site may have to be used, which will be on the Sharepoint server as well...

    Thanks for the help, I really need this stuff sorted out!

  • Larkin Y

    I presume you are writing this code in a Windows Forms app or something

    Context is used for webparts, so the code knows which sharepoint site it is running in. If you are using a Windows Forms app, it must be running on the server, and you'll need to supply the url of the sharepoint site. If your app is not running on the server, you'll need to use the sharepoint webservices.

    Hope that helps
    Nick



  • dHan61

    I'm not sure why IIS would appear to be stopped after running that code; but I can explain why this code works and the first doesn't. The "Context" in the code is an HttpContext, and is only valid when your code is running within an ASP.NET application. When you're writing a standalone app you need to use the SPSite constructor like you have in the second code snippet.
  • wcpc_el

    well as it turns out, you probably dont need to help with the code that much, as for some reason, I couldnt get it to compile before but now I can...

    here is what I got:

    SPSite site = new SPSite("http://server:12345/sites/Name/");

    using (SPWeb web = site.OpenWeb())
    {
    string docLibName = "Shared Documents";
    string viewName = "All Documents";

    foreach (SPFolder f in web.Folders)
    {
    itemListLiteral.Text += "<b>" + f.Name + "</b><br />";
    foreach (SPFile file in f.Files)
    {
    itemListLiteral.Text += "&nbsp;&nbsp;&nbsp;&nbsp;" + file.Name + "<br />";
    }
    }
    }

    as you can see its a different chunk of code, but it works in what I am trying to do...

    But for some weird reason IIS seems to completely stops working (all website are stopped) after I run this code, would you or anyone know why that is

  • dcument library help