1

Firstly, I'm a newbie to C# and SharePoint, (less than a month's experience) so apologies if this is an obvious or easy question but I've been trawling the net for a couple of days now with absolutely no success.

I have an xslt file that I have stored in a subdirectory of 'Style Library' from within the new website but how can I access this from within c#?

I've looked at SPSite and SPWeb but neither seems able to do quite what I want.

Any and all help will be gratefully received.

Many thanks

c#newbie

pwmusic
  • 4,739
  • 4
  • 23
  • 14
  • Some additional info: We're using .NET 2.0 Framework We will be deploying the code on the SharePoint server Let me know if I've omitted any other pertinent information. – pwmusic Jan 08 '09 at 17:16

5 Answers5

1

Here is a bit of code to retrieve the list items from a list:

SPList list = web.Lists["MyLibrary"];
            if (list != null)
            {
                var results = from SPListItem listItem in list.Items
                              select new 
                              {
                                  xxx = (string)listItem["FieldName"]),
                                  yyy  = (string)listItem["AnotherField"],
                                  zzz = (string)listItem["Field"]
                              };
            }

To retrieve a file you could also use this method on SPWeb: GetFileAsString

Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
  • Hi Ray Thanks very much for replying so quickly! What type of variable is 'web' and how should it be instantiated to give me access to the 'Style Library'? Also, we're developing under 2.0 .NET Framework so I can't use LINQ (but that's the least of my worries right now!) Thanks again Patrick – pwmusic Jan 08 '09 at 16:41
  • Web is of SPWeb type. http://msdn.microsoft.com/en-us/library/ms473633.aspx this is and article from MSDN which provides simple overview of Sharepoint's object model. So style library is just standard SPList object which can be accessed from SPWeb object – Marian Polacek Jan 12 '09 at 18:57
  • Sorry, my fault, it is an SPWeb. Don't forget to push this into a using statement as it implements IDisposable. – Ray Booysen Jan 12 '09 at 22:49
0

Patrick,

I hope you enjoy both C# and SharePoint!

Check out the article here.

Read that through, and it should give you all the assistance you need.

Nick.

Nick
  • 2,285
  • 2
  • 14
  • 26
  • Hi Nick Many thanks for your speedy reply. I'm not sure the techniques suggested in the link can be used for my purposes as my code is to be deployed on the SharePoint server and the notes seem to be aimed at those not wanting to deploy code on the SharePoint server itself. – pwmusic Jan 08 '09 at 16:38
0

without linq:

int itemId = getItemId();
SPWeb currentWeb = SPContext.Current.Web;
SPList list =  currentWeb.Lists["MyList"];
if ( list != null )
{
     SPListItem theItem = list.Items.GetItemById(itemId);
     doWork(theItem);
}

The SPWeb can be retrieved in numerous ways, using the SPContext will work if the code is called from SharePoint. To get an SPWeb object from a URL you can use SPSite object i.e.

using ( SPSite site = new SPSite(urlToWeb) )
{
   using (SPWeb web = site.OpenWeb())
   {
     doWork(web);
   }
}

the 'using' statement ensures non-managed resources are reclaimed in a timely manner, by calling 'Dispose()' on the relevant objects.

HTH, jt

Jason
  • 15,915
  • 3
  • 48
  • 72
0

Many thanks for your assistance with this. I've used a little bit from each and done some additional reading and have come up with the following:

private static string getXsl()
{
    string xslString = null;
    using (StreamReader streamReader = new StreamReader(
        File.Open(HttpContext.Current.Server.MapPath(@"~_layouts\theXSL.xslt"), FileMode.Open)))
    {
        xslString = streamReader.ReadToEnd();
    }
    return xslString;
}
takrl
  • 6,356
  • 3
  • 60
  • 69
pwmusic
  • 4,739
  • 4
  • 23
  • 14
0

Effective as that may be, you should really look into best practices as they relate to storing documents in the 12 hive versus the content database.

There are much more scalable answers, which should be considered before you choose the lemming route.

user15916
  • 21
  • 1