Accessing a SharePoint style library programmatically from C #
Firstly, I am new to C # and SharePoint (less than a month), so apologize if this is an obvious or simple question, but I was wasting the web for a couple of days without much success.
I have an xslt file that I saved in the Styles Library subdirectory from a new website, but how can I access this from within C #?
I have looked at SPSite and SPWeb, but neither can do what I want.
Any help would be greatly appreciated.
Many thanks
C # newbie
source to share
Here's some code to extract 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 extract the file, you can also use this method in SPWeb: GetFileAsString
source to share
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);
}
SPWeb can be retrieved in a variety of ways using SPContext, will work if the code is called from SharePoint. To get SPWeb object from url you can use SPSite object ie
using ( SPSite site = new SPSite(urlToWeb) )
{
using (SPWeb web = site.OpenWeb())
{
doWork(web);
}
}
the 'using' statement ensures that non-managed resources are fixed in a timely manner by calling Dispose () on the appropriate objects.
NTN, JT
source to share
Thanks a lot for your help with this. I used a bit of each and did some additional reading and came 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;
}
source to share