Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top