Ejemplo n.º 1
0
        public override bool Write(string fileName, byte[] data)
        {
            try
            {
                SharepointClientContext.Load(SharepointList.RootFolder);
                SharepointClientContext.ExecuteQuery();

                string fileUrl = String.Format("{0}/{1}", SharepointList.RootFolder.ServerRelativeUrl, fileName);
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(_clientContext, fileUrl, new MemoryStream(data, false), true);
                _clientContext.ExecuteQuery(); //Uploaded .. but still checked out...


                //Load the FieldCollection from the list...
                SP.FieldCollection fileFields = SharepointList.Fields;
                _clientContext.Load(fileFields);
                _clientContext.ExecuteQuery();

                SP.File     uploadedFile    = _web.GetFileByServerRelativeUrl(fileUrl);
                SP.ListItem newFileListItem = uploadedFile.ListItemAllFields;
                newFileListItem.Update();
                _clientContext.ExecuteQuery();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public ActionResult ScanTemplates(List <string> urls)
        {
            var spContext = SharePointContextProvider.Current.GetSharePointContext(System.Web.HttpContext.Current);
            var scanInfo  = new ScanTemplateInfo();
            var listInfo  = new List <FormInformation>();

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                foreach (var url in urls)
                {
                    try
                    {
                        SP.Web web = clientContext.Site.RootWeb;

                        var file = web.GetFileByServerRelativeUrl(url);

                        FormInformation info = InfoPathAnalytics.FormInformation(clientContext, file);
                        info.XsnUrl = file.ServerRelativeUrl;

                        listInfo.Add(info);
                    }
                    catch
                    {
                        scanInfo.AddMessage(url);
                    }
                }
            }
            scanInfo.FormInfos = listInfo;

            return(View(scanInfo));
        }
Ejemplo n.º 3
0
    private void GetDocIDURL(ref StringBuilder permaurl)
    {
        string ctxurl = permaurl.ToString(0, 24);
        string relurl = permaurl.Replace("https://sp.contoso.com", "").ToString();

        // Starting with ClientContext, the constructor requires a URL to the
        // server running SharePoint.
        SP.ClientContext context = new SP.ClientContext(ctxurl);
        // The SharePoint web at the URL.
        SP.Web web = context.Web;
        // Load
        context.Load(web);
        // Execute query.
        context.ExecuteQuery();
        //
        SP.File     ObjFile = web.GetFileByServerRelativeUrl(relurl);
        SP.ListItem item    = ObjFile.ListItemAllFields;
        //
        context.Load(ObjFile);
        context.Load(item);
        context.ExecuteQuery();
        //
        //string fileName = item.FieldValues["FileLeafRef"].ToString();
        //string fileType = System.IO.Path.GetExtension(fileName);
        //Guid uniqueId = new Guid(item.FieldValues["UniqueId"].ToString());
        var furl = item.FieldValues["_dlc_DocIdUrl"] as SP.FieldUrlValue;

        permaurl.Clear();
        permaurl.Append(furl.Url);
    }
Ejemplo n.º 4
0
        private static bool TryGetFileByServerRelativeUrl(SP.Web web, string serverRelativeUrl, out SP.File file)
        {
            var ctx = web.Context;

            try
            {
                file = web.GetFileByServerRelativeUrl(serverRelativeUrl);
                ctx.Load(file);
                ctx.ExecuteQuery();
                return(file.Exists);
            }
            catch
            {
                file = null;
                return(false);
            }
        }
Ejemplo n.º 5
0
        private static List <Tuple <string, Stream> > GetXmlFilesFromList()
        {
            List <Tuple <string, Stream> > inputFileStreams = new List <Tuple <string, Stream> >();  // XML files (item1=Name, item2=Data)

            spClient.ClientContext spCtx = LoginCsom();

            spClient.Web myWeb = spCtx.Web;
            spClient.FolderCollection   myFolders = myWeb.Folders;
            spClient.List               myList    = spCtx.Web.Lists.GetByTitle(SpListName);
            spClient.ListItemCollection allItems  = myList.GetItems(spClient.CamlQuery.CreateAllItemsQuery());
            spCtx.Load(myWeb);
            spCtx.Load(myFolders);
            spCtx.Load(allItems);
            spCtx.ExecuteQuery();

            foreach (spClient.ListItem oneItem in allItems)
            {
                spClient.AttachmentCollection allAttachments = oneItem.AttachmentFiles;
                spCtx.Load(allAttachments);
                spCtx.ExecuteQuery();

                foreach (spClient.Attachment oneAttachment in allAttachments)
                {
                    spClient.File myXmlFile = myWeb.GetFileByServerRelativeUrl(oneAttachment.ServerRelativeUrl);
                    spClient.ClientResult <Stream> myXmlData = myXmlFile.OpenBinaryStream();
                    spCtx.Load(myXmlFile);
                    spCtx.ExecuteQuery();

                    using (MemoryStream mStream = new MemoryStream())
                    {
                        if (myXmlData != null)
                        {
                            myXmlData.Value.CopyTo(mStream);
                            byte[]       myBinFile = mStream.ToArray();
                            MemoryStream xmlStream = new MemoryStream(myBinFile);
                            inputFileStreams.Add(Tuple.Create(myXmlFile.Name, (Stream)xmlStream));
                        }
                    }
                }
            }

            return(inputFileStreams);
        }
Ejemplo n.º 6
0
        public ActionResult ScanTemplate()
        {
            var    spContext    = SharePointContextProvider.Current.GetSharePointContext(System.Web.HttpContext.Current);
            string spListId     = this.Request["SPListId"];
            string spListItemId = this.Request["SPListItemId"];
            string relativeUrl  = this.Request["url"];

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                SP.File file = null;

                SP.Web web = clientContext.Web;
                clientContext.Load(web);

                if (string.IsNullOrEmpty(relativeUrl))
                {
                    Guid listId = new Guid(spListId);
                    int  itemId = Convert.ToInt32(spListItemId);

                    SP.List spList = clientContext.Web.Lists.GetById(listId);
                    clientContext.Load <SP.List>(spList);

                    SP.ListItem item = spList.GetItemById(itemId);
                    clientContext.Load <SP.ListItem>(item);
                    clientContext.ExecuteQuery();
                    file = item.File;
                }
                else
                {
                    file = web.GetFileByServerRelativeUrl(relativeUrl);
                }
                FormInformation info = InfoPathAnalytics.FormInformation(clientContext, file);
                info.XsnUrl = file.ServerRelativeUrl;
                return(View(info));
            }
        }
Ejemplo n.º 7
0
        public bool DeleteFile(string fileName, string folderName)
        {
            bool   result   = false;
            string siteUrl  = SLMConstant.Ecm.SiteUrl;      //http://ecm/dept/public
            string sitePath = SLMConstant.Ecm.SitePath;     ///dept/public/OBTDocument/
            string domain   = SLMConstant.Ecm.Domain;
            string username = SLMConstant.Ecm.Username;
            string password = SLMConstant.Ecm.Password;

            try
            {
                using (ToEcm.ClientContext clientContext = new ToEcm.ClientContext(siteUrl)
                {
                    Credentials = new NetworkCredential(username, password, domain)
                })
                {
                    string serverRelativeUrlOfFile = sitePath + folderName + "/" + fileName;

                    log.Info("Delete File Path " + serverRelativeUrlOfFile);

                    ToEcm.Web web = clientContext.Web;
                    clientContext.Load(web);

                    ToEcm.File file = web.GetFileByServerRelativeUrl(serverRelativeUrlOfFile);
                    clientContext.Load(file);
                    clientContext.ExecuteQuery();

                    if (file != null)
                    {
                        file.DeleteObject();
                        clientContext.ExecuteQuery();
                    }
                    result = true;

                    //Added By Pom 24/05/2016
                    //ลบ Folder ทิ้งในกรณีที่ไม่มี file เหลือใน Folder นั้น
                    try
                    {
                        log.Info("Start Delete Folder " + sitePath + folderName + "/");
                        ToEcm.Folder folder = web.GetFolderByServerRelativeUrl(sitePath + folderName + "/");
                        clientContext.Load(folder);
                        clientContext.ExecuteQuery();

                        if (folder != null)
                        {
                            ToEcm.FileCollection fileColl = folder.Files;
                            clientContext.Load(fileColl);
                            clientContext.ExecuteQuery();

                            log.Info("Files currently in folder " + folderName + " = " + fileColl.Count.ToString());

                            if (fileColl.Count == 0)
                            {
                                folder.DeleteObject();
                                clientContext.ExecuteQuery();
                                log.Info("Folder Deleted Successfully");
                            }
                        }
                        else
                        {
                            log.Info("Folder " + folderName + " is null");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error DeleteFile : Cannot delete empty folder " + folderName + ", " + ex);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Error DeleteFile : " + ex);
            }

            return(result);
        }