Getting files from Sharepoint using .Net - c#

Good afternoon, I am using the Microsoft.SharePointOnline.CSOM library to work with Sharepoint. I have a code where I get a list called MyDoc like this:
using (var context = new ClientContext(url)
{
varweb = context.Web;
var list = web.Lists.GetByTitle("MyDocs");
}
Then I iterate through all the folders to find a folder with a suitable name and get files from there. With the help of file.ServerRelativeUrl I found out the link to the file on Sharepoint:
/MyDocs/Documents/Students/Homework/1lesson.pdf
How can I immediately access the Homework folder and download all the files from there without going through all the possible folders in the MyDocs sheet?

Instead of getting the list, just use the method GetFolderByServerRelativeUrl within the context.Web object
var folder = context.Web.GetFolderByServerRelativeUrl("/MyList/MyFolder");
context.Load(folder);
context.ExecuteQuery();

Related

Read an xml file from non-web project in C# (Visual Studio)

I am trying to read an xml file that is present in one of the projects in my VS solution. Here's my code.
string path = HttpContext.Current.Server.MapPath(#"~\Resources\XmlDocument.xml");
XDocument document = XDocument.Load(path);
This works fine if my xml file is in the web project. But in reality I have my xml file in a different project under the same solution.
I cannot use Server.MapPath as this searches web project. I searched for an answer but did not find any solution that worked for me.
How can I access this xml file? I am trying to access this from a helper method in the same project.
Try this:
string path = HostingEnvironment.ApplicationPhysicalPath + (some path);
public void LoadXML()
{
if (System.IO.File.Exists(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<int>));
lock (new object())
{
using (TextReader reader = new StreamReader(path))
{
return (List<int>)(serializer.Deserialize(reader));
}
}
}
}
this is working for me.
Once you get your current directory, why not just navigate up a number of parent directories and then locate your file?
DirectoryInfo info = new DirectoryInfo(Environment.CurrentDirectory);
DirectoryInfo parent = info.Parent.Parent;
string file = Path.Combine(parent.FullName, #"your\path");
Here I use Environment.CurrentDirectory to get where the program is at now, but you can stack as many Parent elements as you need. Then you can combine the path to get to your file in the other project.
EDIT:
Once your application is running all the environment variables and Server.MapPath will give you the location that it is executing from (i.e. the path you are getting now). In light of this it may be easier to create a folder and reference this via it's full path. (C:\Data\myfile.xml)

where are the dictionary files stored in visual studio

I am trying to make an application which checks whether a word is a valid dictionary word.
NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary();
oDict.DictionaryFile = "en-US.dic";
oDict.Initialize(); // file not found exception
string wordToCheck = "door";
NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling();
oSpell.Dictionary = oDict;
if(oSpell.TestWord(wordToCheck))
{
//Word exist in dictionary
...
}
I have tried giving all possible file locations like ".\en-US.dic" to "C:\Program Files\IIS Express\en-US.dic" but i still get file not found exception. Can anyone help me in figuring where the file actually is.
When you install netSpell using nuget a new folder is created by the name 'Packages'. All the .dic (dictionary) files are stored in that folder.
You need to use a relative path using Server.MapPath:
If the file en-US.dic is in your root directory, use:
oDict.DictionaryFile = Server.MapPath("en-US.dic");
If the file en-US.dic is nested under some other directory, use:
oDict.DictionaryFile = Server.MapPath("/SomeDirectory/en-US.dic");

ASP.NET Get all pages of website

I want to parse some websites and get list of all pages on current domain, like a:
sample.com/
sample.com/page1/
sample.com/page2.html
But I can't find samples, how to build this sitemap or tree using C# and ASP.NET
I found only one example:
http://www.codeproject.com/Articles/13486/A-Simple-Crawler-Using-C-Sockets
But I can't understand, how author use it
if(Directory.Exists(strUri) == true)
{
//some code
DirectoryInfo dir = new DirectoryInfo(folderName);
FileInfo[] fia = dir.GetFiles("*.txt");
}
When I use this code, result in if is always false. When I use only
GetFiles function
I have Exception:
URI formats are not supported
Who have any ideas?
remember that on a Web ambient, you cant read files that way, you need to use
Server.MapPath(url)
to get the physical address to the files, then you can do the loop you are using.

Rackspace CloudFiles C# API: How to list files on root 'folder', and how to list (sub)folders within a 'folder'?

(1)
I can list the files on a folder this way:
var parameters = new Dictionary<GetListParameters, string>();
parameters.Add(GetListParameters.Path, "folder1/"); // get items from this specific path
var containerItemList = connection.GetContainerItemList(Settings.ContainerName, parameters);
However, this:
parameters.Add(GetListParameters.Path, "/");
or this:
parameters.Add(GetListParameters.Path, "");
does not work.
How can I query the files on the root folder?
(2)
The code above returns the list of files in a folder.
How can I get the list of folders within a folder? I there any parameter I can set to get this list?
Note: I know that this is a 'flat' file system, similar to Amazon S3. However, both (cloudfiles and S3) provides a way to work with 'folder'. In S3 is easy. In cloudfiles (with the .net API) I could not find how to do this.
Any hint will be highly appreciated.
This has just been fixed with the latest push and closes issue #51 on github
Link to downloadable package
Hope this helps.

How to programatically create a folder at a Sharepoint site

this question is related with this one. How can I create a folder to put the files I upload inside them without using the SharePoint API (I'm executing the application on another host)?
Thanks in advance.
SPSite _MySite = new SPSite("http://adfsaccount:2222/");
SPWeb _MyWeb = _MySite.OpenWeb();
SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary) _MyWeb.Lists["My Documents"];
SPFolderCollection _MyFolders = _MyWeb.Folders;
_MyFolders.Add("http://adfsaccount:2222/My%20Documents/" + txtUpload.Text + "/");
_MyDocLibrary.Update();
_MyWeb.Dispose();
_MySite .Dispose();
I created console application to create folders. I need to give seperate permissions for each and every folder because those are all state folders. So you can look at the breakroleinheritence property also.

Categories