views:

4095

answers:

6

Hi all

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

+1  A: 

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
Hi RayThanks 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 againPatrick
Patrick
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
drax
Sorry, my fault, it is an SPWeb. Don't forget to push this into a using statement as it implements IDisposable.
Ray Booysen
A: 

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
Hi NickMany 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.
Patrick
A: 

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
A: 

Hi guys

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;
}

Patrick
A: 

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.

A: 

To understand what you can do programmatically in SharePoint, You must have understanding of SharePoint object model.

To know more about SharePoint Object Model, please visit: http://www.etechplanet.com/post/2009/08/28/Overview-of-SharePoint-Object-Model.aspx

Technology Professional