Hot answers tagged

22

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 ...


9

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 ...


6

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


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

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 ...


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

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


3

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...


3

I'm grasping at straws here, there isn't a lot of information on the topic about this. I did stumble upon a post that may give clues as to why it is happening and perhaps a work around: SPFile.MoveTo(), at first glance, looks to be a very straightforward method where a file can be moved from one folder to another. Trying to use it and have an ...


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 = ...


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


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

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 ...


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

I figured it out. You can use SPFile to check if the file exist in the library and then move it if it does. SPFile file = site.GetFile("/Final Candidates/" + CName); if (file.Exists) { file.MoveTo("Draft Candidates/" + CName, true); } Reference: http://stackoverflow.com/questions/359672/how-to-determine-if-a-file-exists-in-a-sharepoint-spfolder


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

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

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


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


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

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

Try This, Declare a Client context ctx and get your list/library name: RoleDefinition roleDef = ctx.Web.RoleDefinitions.GetByType(RoleType.Reader); RoleDefinitionBindingCollection roleDefBinding = new RoleDefinitionBindingCollection( ctx ); ctx.ExecuteQuery(); roleDefBinding.Add( roleDef ); CamlQuery query; query = ...


1

Try to get a new instance of the SPFile object right before you try the .CheckOut() instruction. I may be wrong, but I think you are experiencing a pretty common transaction related issue. if (file != null) { web.AllowUnsafeUpdates = true; Stream readStream = file.OpenBinaryStream(); //file is SPFile type SPFile ...


1

AFAIK, nothing in the client-side API can allow this. However, there's an old technology called "Front-Page RPC" (the name itself shows you how old it is!) that may be still available on SP2013 (even if it's kind of deprecated/not really documented). It is still there since it's used by Office applications to communicate with SharePoint. FP RPC is a ...


1

Use ItemAdding to prepare file for uploading and ItemAdded to kick off bussiness logic. This means, in your case, I would put all the code in ItemAdding event. In this way you can utilize properties.AfterProperties to rename file. properties.AfterProperties["Title"] = szNewFileName; Also, I'd recommended to access SPWeb as SPWeb web = ...


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

You might not have permissiosns to see that file. You should also add a check for file existence like : SPFile u = l.RootFolder.Files.Add(l.RootFolder.Url + "/" + f.Name, f.OpenBinary(), true); if (l.Exists) { l.RootFolder.Update(); l.Update(); w.Update(); }


1

I found an answer here. Users with contributor permissions are prevented from uploading files with the following extensions: ASPX; MASTER; XAP; SWF; JAR; ASMX; ASCX; XSN; XSF; So my problem is in "aspx" extension of file I'm trying to create. This could be fixed by removing 'aspx' from WebApplication.WebFileExtensions ...


1

SPListItemCollection is a collection of SPListItem. Get File object from SPListItem. Example: foreach (SPListItem item in items) { SPFile file = item.File; }



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