Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
15Activity
0 of .
Results for:
No results containing your search query
P. 1
Share Point Sample Code

Share Point Sample Code

Ratings: (0)|Views: 1,340|Likes:
Published by semalaiappan

More info:

Published by: semalaiappan on Apr 17, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

01/26/2011

pdf

text

original

 
SharePoint Tips & Code
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.Itemsselect 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
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 fromSharePoint. 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.Effective as that may be, you should
really
look into best practices as they relate to storing documents inthe 12 hive versus the content database. private static string getXsl(){string xslString = null;using (StreamReader streamReader = newStreamReader(File.Open(HttpContext.Current.Server.MapPath(@"~_layouts\theXSL.xslt"),FileMode.Open))){xslString = streamReader.ReadToEnd();}
Page | 1
 
SharePoint Tips & Code
return xslString;}
The technical answer to your original question is a qualified "No": all SPWeb objects opened from anSPSite are automatically disposed when the SPSite is disposed. However, in practice it is a good idea todispose an SPWeb as soon as you're done with it to reduce memory pressure, especially when workingwith code like this that opens several SPWeb objects.Implementing this dispose-safe behavior for LINQ is actually quite simple in C#. You can find full detailsin this post,but the short version is that a C# iterator can handle disposal for you. Using my AsSafeEnumerable() extension method, your code is relatively safe written like this:using (SPSite spSite = Utility.GetElevatedSite(_rootUrl)){var sw = from SPWeb web in spSite.AllWebs.AsSafeEnumerable()where web.ServerRelativeUrl.ToLower() == pathfrom SPWeb subWeb in web.Webs.AsSafeEnumerable()select subWeb;foreach(SPWeb aSubWeb in sw){// Do something}} Now the result of your query, which I've assigned to sw, is a lazy iterator of type IEnumerable<SPWeb>.As you enumerate over that result, each SPWeb will be disposed when the enumerator moves to the nextitem. This means that it is not safe to use that SPWeb reference, or any SP* object created from it (SPList,etc), outside of your foreach loop. Also sw would not be safe to use outside of that using block becausethe iterator's SPWebCollections will be tied to the now-disposed SPSite.That said, code like this that enumerates over all webs (twice!) is extremely expensive. There is almostcertainly a more efficient way that this could be implemented, if only by using spSite.AllWebs[path]instead of your from/where.Regarding garbage collection, these objects require disposal because of unmanaged memory allocatedthat the GC doesn't even know about.Finally, a word of caution about your 'GetElevatedSite' utility. If you're using RunWithElevatedPrivilegesin your helper method to get your elevated SPSite, there are a number of issues you could run into byreturning your SPSite out of that elevated context. If possible, I would suggest using SPSiteimpersonation instead - my preferred method is describedhere.Q: When should you dispose SPWeb and SPSite objects? And even more important, when not?
Page | 2
 
SharePoint Tips & Code
A: You should always dispose them if you created them yourself, but not otherwise. You should never dispose SPContext.Current.Web/Site and you should normally not dispose SPWeb if IsRootWeb is true.More tricky constructs are things along the line of SPList.ParentWeb.
You can write some custom code to do it. You could use the SharePoint API if you are on the same server or use WebServicesHere is the sample code assuming that you know the url of the document library and you are uploadingthe document to the root folder. You will have to add Microsoft.SharePoint.dll as reference to your ASP.NET projectusing (SPSite siteCollection = new SPSite(url)){using (SPWeb spWeb = siteCollection.OpenWeb()){SPList spList = spWeb.GetList(url);string fileName = "XXXX";FileStream fileStream = null;Byte[] fileContent = null;try{string docPath = XXXX; //physical location of the filefileStream = File.OpenRead(docPath + fileName);fileContent = new byte[Convert.ToInt32(fileStream.Length)];fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + fileName, fileContent, true);spList.Update();}catch(Exception ex){}finally{if (fileStream != null){fileStream.Close();}}}}
Page | 3

Activity (15)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
ALi liked this
ALi liked this
dayooyefuga liked this
Lukmann liked this
Fiodir Eissayou liked this
funfrom83 liked this
Esha Jain liked this
Murugan liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->