Tag Info

Hot answers tagged

20

If you know the SPWeb but not the document library, you can use SPWeb.GetFile: SPFile file = web.GetFile(guid);


9

Simpliest case Consider you know that document is stored in standard "Shared Documents" library from the Team Site Template, you should use this code: var library = web.GetListFromUrl("http://site/Shared%20Documents/Forms/AllItems.aspx"); var file = library.Items[new Guid("137DA01F-9AFD-5d9d-80C7-02AF85C822A8")].File; // file.Url - site-relative url of the ...


7

SharePoint has great built in methods to copy or move documents across sub-sites or lists within the same site collection. You can use SPFile.MoveTo or SPFile.CopyTo. You can even move whole folders and their contents with the SPFolder.MoveTo method. Very nice. Unfortunately, these do not work across site collections. where as use of RootFolder.Files.Add ...


5

There's the SPList.GetItemByUniqueId function, but if you don't know which list to search you're out of luck. If you're using SharePoint 2010, look at the Document ID functionality (which attaches a unique ID to a list item and then uses Search to find the item). Depending upon your requirements, you might consider implementing ...


5

Use the exists property instead of null, GetFile method doesn't return null if file doesn't exist SPFile file = web.GetFile(fileurl); if(file.Exists) { //code here }


5

You can use the SPWeb GetFile method to retrive the SPFile located at a specified url. Try something like this: SPFile file = SPContext.Current.Web.GetFile(string.Format("{0}/{1}", x, y));


4

Very similar to above, the SPSite object does actually remember the URL it was opened with - so you don't have to give a Web name. You should be able to use something like: string url= "Full URL to File"; using (SPSite site = new SPSite(url)){ using (SPWeb web = site.OpenWeb()) { SPFile file = web.GetFile(url); SPListItem item = ...


3

In above code SPWeb.GetFile don't give you a file as it does not exist (because you are copying a new file) SPFile.SaveBinary is normally used to update exisitng file. To add a new file you should use SPFolder.Files.Add


3

UPDATE When using SPListItem from a SPQuery, to access .File properties ensure you have fully loaded the item item = SPList.GetItemById(item.Id) ORIGINAL (Wrong) CheckOutType is an enumeration (value type) so can't be null. http://msdn.microsoft.com/en-us/library/s1ax56ch%28VS.80%29.aspx My guess is that item.File is null Try using: SPFile file = ...


2

I am thinking of giving ICustomMapping a go to create a ListItem property on my Entities of type SPListItem and use MapFrom() set it to be the item which it represents. I am just worried about object lifetime/disposal issues, i.e. I'm not sure if the ancestor path of the Item's SPList (SPWeb>SPSite) is still open after the Entity's properties have been ...


2

please check this link it may helpfull to you http://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint thanks


2

You need to specify more than just the attachment file name in the SPWeb.GetFile method! Specify the Web relative file URL or the server relative file URL here. You can construct the Web relative file URL from source.Attachments.UrlPrefix + file


2

Add the MemoryStream as a byte[]: folder.Files.Add(fileName, memoryStream.ToArray(), bool);


2

This is a simple approach I have used in the past with a try/catch so maybe you could revamp for your needs: string myResponse = "whatever needs to be written to the file..."; string filename = "C:\\splogs\\" + (DateTime.Now).ToString("yyyyMMdd") + "_customlog.log"; FileStream logStream = new FileStream(@filename, FileMode.Append); ...


2

SPFileCollection is probably a bad idea. I suppose it's too coupled to the internal SharePoint storage behavior. Replace it with a simple List<SPFile>. This will be easier and works. A side note, your approach is not taking care of files in subfolders. You should write a small recursive method to get all documents : private ...


2

in the ItemUpdating ER the value properties.AfterProperties["vti_contenttag"] is null when the doc is added, overwritting another one. This could be a way to go, not the best for sure, but still a working solution...


2

Oh man, SPList.Folders actually returns an SPListItemCollection, not an SPFolderCollection... However, SPList.RootFolder.SubFolders does return an SPFolderCollection...


2

Blockquote I don't like using [0] to get the SPListItem...interested in reccomendations Well, SPList.Items[0] syntax fetch ALL THE DATA from SharePoint and after - give you item by index. That's a common behavior. You might use also SPList.GetItemById or similar methods or even make a caml query against the target list w/ SPList.GetItems method. It's ...


1

Verify that your $file.Name is truly "myfile.docx". Try these amended lines (with the other lines as is): $DestUrl ="MyDocuments/SubFolderName/" $file.MoveTo($DestUrl + $file.Name, true)


1

ItemUpdating allows you to get the file before the upload/action has taken place. ItemUpdated requires you to go through version history. In code you can get the previous version as follows: SPFile file = ...; SPFileVersion previousVersion = file.Versions[file.Versions.Count - 1];


1

MS recommends waiting 10 mins or rebooting the server. http://support.microsoft.com/kb/899709/en-us Sometimes (mostly) that won't work so try out this code. http://www.codeproject.com/Articles/93965/Force-SharePoint-Document-Unlocked-Checked-In


1

I dont think there is a seperate flag for this, maybe you can use BeforeProperties and AfterProperties to achieve some workaround, like checking if only the version is increased and no other field has changed, you can consider this case as overwritten, not fullprrof not the best way but maybe one of the solutions


1

I would recomand you read this article from Waldek concerning problems revolving with web.GetFile() available at http://blog.mastykarz.nl/inconvenient-spwebgetfilestring/ and confront with your code, particularly when you passing a certain URL to the method.


1

I found a partial solution to this by using this code (DestinationFile is my own extension method used to get the new file): SPList list = web.Lists[listId]; SPListItem item = list.GetItemById(itemId); SPFile newFile = item.DestinationFile(); // Extension method to get the published file string newUrl = item.File.Url.Replace(item.File.Name,newFile.Name); ...


1

Thank you all for your answers. You wouldn't believe how silly and hard to find this error was. The myLibrary's Url was: /Library/Documents while the siteUrl, that was passed to my method was http://localhost:9999/ This resulted in double slash between server name and list relative path.


1

The reason you're seeing this is because of references crossing each other. You say destinationFile is an SPFile. You call destinationFile.Item.SystemUpdate(false). Which is fine. You have to watch though, because SPFile.Item returns a reference to an SPListItem. If you call this twice, you get a different reference to the SPListItem. Therefore, if you ...


1

I don't know if this could be the cause of your problem, but by looking at your code I noticed that you use the istruction SPFile page = web.GetFile(url); If this is the case (and it isn't a simple copy&paste error) be aware that you are breaking the elevation process. By looking at your code I would assume that you already know this, but you should be ...


1

Get your Web var web = SPContext.Current.Web Create your file location var location = Path.Combine(entity.Path, entity.Name) call SPFile file = _web.GetFile(_location) Easier if you write it as a static extension method for the SPFile class.


1

There's the SPMetal Extender on CodePlex which might help, but I think you're gonna have to extend the entity further by adding a property to it which returns a Stream, and is effectively a proxy call to the object model. Of course, as soon as you need to run SPMetal again, you'll loose the custom property. Then again, you might be able to create an ...


1

We have a similar problem that we figured out. When we call: SPList list = web.GetListFromUrl(...) foreach(SPItem item in list.Items) { if (item.File.CheckOutType == SPFile.SPCheckOutType.None) { //Something else here; } } in rare cases and for a specific item id (lets say item id = 524) the code will always run into a deadlock. It means the ...



Only top voted, non community-wiki answers of a minimum length are eligible