You must Sign In to post a response.
  • Category: Sharepoint

    How to attach files from local folder into sharepoint list using c# and timer job to it

    Hello vetarans,

    1)How to attach files from local folder into sharepoint list using c# and add timer job to it.
    to get files continuosly in to list.
    2) i tried it but one error is "Access denied to path in sharepoint".
    Make i know the exact solution for this.

    Here is my Code:-
    ---------------------------
    public void UploadFile()
    {
    try
    {


    using (SPSite oSite = new SPSite(destUrl))
    {
    SPWeb oWeb = oSite.OpenWeb();

    SPList oList = oWeb.Lists["IncomingFax"];
    foreach (FileInfo oFI in new DirectoryInfo(SelectedfilePath).GetFiles())
    {
    oList.EnableAttachments = true;

    string destFileUrl = oList.RootFolder.ServerRelativeUrl+"/"+ oFI.Name;

    oWeb.AllowUnsafeUpdates = true;
    using (FileStream fileStream = new System.IO.FileStream(SelectedfilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
    {
    oWeb.Files.Add(destFileUrl, fileStream, true/*overwrite*/);
    fileStream.Close();
    }
    }

    }
    }
    catch (Exception ex)
    {
    ex.ToString();
    }
    }
    ---------------------------
  • #763672
    Hai Saritha,
    This is very common error when working with the SharePoint sites.
    To get rid of this issue, you need to use SPSecurity.RunWithElevatedPrivileges which will provide the same privileges to the site and web including the list and libraries.
    Below is the link where you can get the snippet of code to be modified accordingly:

    SPSecurity.RunWithElevatedPrivileges(delegate
    {
    using(var site = SPContext.Current.Site.ID)
    {
    using(var web = SPContext.Current.Web.ID)
    {
    // your code to be put here
    }
    }
    });

    Link:

    http://sharepoint.stackexchange.com/questions/54607/sharepoint-access-to-path-is-denied

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments