예제 #1
0
 /// <summary>
 /// </summary>
 public static string MakeSafe(object o)
 {
     return(GenUtil.SafeTrim(o).Replace("'", "''"));
 }
예제 #2
0
        /// <summary>
        /// Only use this for comparisons, never for normalizing data to save in MMD (this is done automatically).
        /// </summary>
        //public static string MmdNormalizeForComparison(object o)
        //{
        //    // SharePoint internally removes multiple spaces, and replaces & and " with unicode equivalents.
        //    return Term.NormalizeName(MmdRemoveIllegalChars(SafeTrim(o)));
        //}

        /// <summary>
        /// Not used.
        /// </summary>
        public static string MmdDenormalize(object o)
        {
            return(GenUtil.SafeTrim(o)
                   .Replace(Convert.ToChar(char.ConvertFromUtf32(65286)), '&')
                   .Replace(Convert.ToChar(char.ConvertFromUtf32(65282)), '"'));
        }
예제 #3
0
        /// <summary>
        /// </summary>


        /// <summary>
        /// </summary>
        public static bool GetListFoldersFilesRootLevel(SpConnectionManager spConnection,
                                                        Guid?listId,
                                                        int sortCol,
                                                        int rowLimit,
                                                        out string rootFolderPath,
                                                        out List <SpFileSystemItem> lstObjs,
                                                        out string msg)
        {
            msg            = "";
            lstObjs        = new List <SpFileSystemItem>();
            rootFolderPath = "";

            // make sure caml row limit is never more than 5000
            var camlRowLimit = rowLimit > Consts.MAX_ROW_LIMIT ? Consts.MAX_ROW_LIMIT : rowLimit;
            var i            = 0;

            try
            {
                using (var ctx = spConnection.GetContext())
                {
                    var list = ctx.Web.Lists.GetById(listId.Value);

                    ctx.Load(list, x => x.Title);
                    ctx.Load(list.RootFolder, x => x.ServerRelativeUrl);
                    ctx.ExecuteQuery();

                    rootFolderPath = list.RootFolder.ServerRelativeUrl;

                    ListItemCollectionPosition pos = null;

                    while (true)
                    {
                        var cq = new CamlQuery();

                        cq.DatesInUtc = false;

                        var strViewFields = "<FieldRef Name='ID' /><FieldRef Name='FileLeafRef' /><FieldRef Name='FileDirRef' /><FieldRef Name='FileRef' /><FieldRef Name='FSObjType' /><FieldRef Name='Created' /><FieldRef Name='Modified' /><FieldRef Name='File_x0020_Size' />";
                        var strQuery      = "<Query></Query>";

                        var strViewXml = "<View Scope='All'>#QUERY#<ViewFields>#VIEWFIELDS#</ViewFields><RowLimit>#ROWLIMIT#</RowLimit></View>"
                                         .Replace("#ROWLIMIT", camlRowLimit.ToString())
                                         .Replace("#QUERY#", strQuery)
                                         .Replace("#VIEWFIELDS#", strViewFields);

                        cq.ListItemCollectionPosition = pos;
                        cq.ViewXml = strViewXml;
                        cq.FolderServerRelativeUrl = list.RootFolder.ServerRelativeUrl;
                        //cq.FolderServerRelativeUrl = "/sites/Zen20/EffLib/folder1"; // for sub folder

                        ListItemCollection lic = list.GetItems(cq);
                        ctx.Load(lic);
                        ctx.ExecuteQuery();

                        pos = lic.ListItemCollectionPosition;

                        foreach (ListItem item in lic)
                        {
                            i++;

                            var fsType = Convert.ToInt32(item["FSObjType"]);
                            if (fsType == 0)
                            {
                                var created  = (DateTime?)GenUtil.SafeToDateTime(item["Created"]);
                                var modified = (DateTime?)GenUtil.SafeToDateTime(item["Modified"]);

                                if (created.Value.Year == 1900)
                                {
                                    created = null;
                                }
                                if (modified.Value.Year == 1900)
                                {
                                    modified = null;
                                }
                                if (!modified.HasValue && created.HasValue)
                                {
                                    modified = created;
                                }

                                var filesize = (int?)GenUtil.SafeToNum(SafeGetFileSize(item));
                                if (filesize == -1)
                                {
                                    filesize = null;
                                }

                                lstObjs.Add(new SpFileSystemItem()
                                {
                                    treeNodeType = NodeType.FILE,
                                    name         = item["FileLeafRef"].SafeTrim(),
                                    url          = item["FileRef"].SafeTrim(),
                                    dtModified   = modified,
                                    length       = filesize
                                });
                            }
                            else
                            {
                                lstObjs.Add(new SpFileSystemItem()
                                {
                                    treeNodeType = NodeType.FOLDER,
                                    name         = item["FileLeafRef"].SafeTrim(),
                                    url          = item["FileRef"].SafeTrim()
                                });
                            }
                        }

                        if (pos == null || i >= rowLimit)
                        {
                            break;
                        }
                    }

                    if (sortCol == 0)
                    {
                        lstObjs = lstObjs.OrderBy(x => x.treeNodeType).ThenBy(x => x.name).ToList();
                    }
                    else if (sortCol == 1)
                    {
                        lstObjs = lstObjs.OrderBy(x => x.treeNodeType).ThenBy(x => x.length).ThenBy(x => x.name).ToList();
                    }
                    else if (sortCol == 2)
                    {
                        lstObjs = lstObjs.OrderBy(x => x.treeNodeType).ThenBy(x => x.dtModified).ThenBy(x => x.name).ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                msg = SHOW_FULL_ERRORS ? ex.ToString() : ex.Message;
            }

            return(msg == "");
        }
예제 #4
0
        public static bool GetDataFromTextFileSimple(string sep, string importTermsPasteBox, string inputTextFile, out List <TermObj> lstTermObjs, out string msg)
        {
            // term guid not imported
            // terms flat in file, no heirarchy
            // can only import terms flat, and term labels for the imported term
            msg         = "";
            lstTermObjs = new List <TermObj>();
            var fileText = "";

            try
            {
                if (!importTermsPasteBox.IsNull())
                {
                    fileText = importTermsPasteBox.Trim();
                }
                else
                {
                    fileText = System.IO.File.ReadAllText(inputTextFile);
                }

                fileText = GenUtil.NormalizeEol(fileText);
                var lines = fileText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();

                foreach (var line in lines)
                {
                    // extract termname and optional labels
                    var termId   = Guid.NewGuid();
                    var termName = "";
                    var labels   = new List <string>();

                    if (line.Contains(sep))
                    {
                        var parts = line.Split(new string[] { sep }, StringSplitOptions.RemoveEmptyEntries).Distinct();

                        if (GenUtil.IsGuid(parts.ElementAt(0)))
                        {
                            // guid is first item, termname must be second (thus 2 item minimum)
                            if (parts.Count() >= 2)
                            {
                                termId   = GenUtil.SafeToGuid(parts.ElementAt(0)).Value;
                                termName = parts.ElementAt(1).Trim();
                                labels   = parts.Skip(2).Where(x => x.Trim().Length > 0).Select(x => x.Trim()).Distinct().ToList();
                            }
                            else
                            {
                                // termname not found, don't import this line
                                termName = "";
                            }
                        }
                        else
                        {
                            termName = parts.ElementAt(0).Trim();
                            labels   = parts.Skip(1).Where(x => x.Trim().Length > 0).Select(x => x.Trim()).Distinct().ToList();
                        }

                        labels.RemoveAll(x => x.Trim().ToLower() == termName.Trim().ToLower());
                    }
                    else
                    {
                        termName = line.Trim();
                    }

                    if (!termName.IsNull())
                    {
                        lstTermObjs.Add(new TermObj()
                        {
                            termId   = termId,
                            termName = termName,
                            labels   = labels
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }

            return(msg == "");
        }
예제 #5
0
        public static bool GetDataFromExcelFileSimple(string inputFile, out List <TermObj> lstTermObjs, out string msg)
        {
            // term guid not imported
            // terms flat in file, no heirarchy
            // can only import terms flat, and term labels for the imported term
            msg         = "";
            lstTermObjs = new List <TermObj>();

            try
            {
                using (ExcelPackage package = new ExcelPackage(new FileInfo(inputFile)))
                {
                    // get the first worksheet in the workbook
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                    var loop = true;
                    int i    = 0;
                    while (loop)
                    {
                        i++;

                        // get first cell, being termname
                        var firstCell = worksheet.Cells[i, 1].Value.SafeTrim();

                        if (firstCell.IsNull())
                        {
                            loop = false;
                        }
                        else
                        {
                            var  termName = "";
                            Guid termId;
                            var  labels = new List <string>();
                            int  j;

                            if (GenUtil.IsGuid(firstCell))
                            {
                                j        = 3;
                                termId   = GenUtil.SafeToGuid(firstCell).Value;
                                termName = worksheet.Cells[i, 2].Value.SafeTrim();
                            }
                            else
                            {
                                j        = 2;
                                termId   = Guid.NewGuid();
                                termName = firstCell;
                            }

                            // make sure termname was found
                            if (!termName.IsNull())
                            {
                                // get labels (optional)
                                while (true)
                                {
                                    var cellj = worksheet.Cells[i, j].Value.SafeTrim();

                                    if (cellj.IsNull())
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        labels.Add(cellj);
                                    }

                                    j++;
                                }

                                labels.RemoveAll(x => x.Trim().ToLower() == termName.Trim().ToLower());

                                lstTermObjs.Add(new TermObj()
                                {
                                    termId   = termId,
                                    termName = termName,
                                    labels   = labels
                                });
                            }
                        }
                    } // while
                }     // using
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }

            return(msg == "");
        }
예제 #6
0
        private static void GetDataAdvWorker(string sep, List <TermObjAdv> lstTermObjs, List <string> lines)
        {
            foreach (var line in lines)
            {
                var termParts = line.Split(new string[] { sep }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable();

                int i       = 0;
                var curPath = "";

                foreach (var termPart in termParts)
                {
                    var curTermPart = termPart;

                    if (curTermPart.IsNull())
                    {
                        continue;
                    }

                    // breakup termpart into [guid]|name|[label0]|[label1]...
                    var termObj  = new TermObjAdv();
                    var termId   = Guid.NewGuid();
                    var termName = curTermPart.Trim();

                    var termSubParts = curTermPart.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable();

                    if (termSubParts.Count() > 1)
                    {
                        int jStart = 0;

                        if (GenUtil.IsGuid(termSubParts.ElementAt(0)))
                        {
                            termId   = Guid.Parse(termSubParts.ElementAt(0).Trim());
                            termName = termSubParts.ElementAt(1).Trim();
                            jStart   = 2;
                        }
                        else
                        {
                            termName = termSubParts.ElementAt(0).Trim();
                            jStart   = 1;
                        }

                        for (int j = jStart; j < termSubParts.Count(); j++)
                        {
                            if (!termSubParts.ElementAt(j).IsNull())
                            {
                                termObj.labels.Add(termSubParts.ElementAt(j).SafeTrim());
                            }
                        }
                    }

                    // fix termname, extract reuse keyword
                    if (termName.ToLower().StartsWith("#reuse") || termName.ToLower().StartsWith("$reuse"))
                    {
                        termObj.isreused    = true;
                        termObj.reusebranch = termName.StartsWith("#reuseall") || termName.StartsWith("$reuseall");

                        termName = Regex.Replace(termName, Regex.Escape("#reuseall"), "", RegexOptions.IgnoreCase);
                        termName = Regex.Replace(termName, Regex.Escape("#reuse"), "", RegexOptions.IgnoreCase);
                        termName = Regex.Replace(termName, Regex.Escape("$reuseall"), "", RegexOptions.IgnoreCase);
                        termName = Regex.Replace(termName, Regex.Escape("$reuse"), "", RegexOptions.IgnoreCase);
                    }

                    // trim labels to unique list
                    termObj.labels = termObj.labels.Distinct().ToList();
                    // remove termname from labels
                    termObj.labels.RemoveAll(x => x.ToLower() == termName.ToLower());

                    curPath += termName + ";";

                    termObj.id       = termId;
                    termObj.termName = termName;
                    termObj.level    = i;
                    termObj.path     = curPath.TrimEnd(";".ToCharArray());

                    // add unique term paths only
                    if (!lstTermObjs.Any(x => x.path.ToLower() == termObj.path.ToLower()))
                    {
                        lstTermObjs.Add(termObj);
                    }

                    i++;
                } // foreach
            }     // foreach
        }
예제 #7
0
        public static bool GetUpdateSimpleDataFromExcelFile(string inputFile, out List <SimpleImportObj> lstObjs, out string msg)
        {
            msg     = "";
            lstObjs = new List <SimpleImportObj>();

            try
            {
                using (ExcelPackage package = new ExcelPackage(new FileInfo(inputFile)))
                {
                    // get the first worksheet in the workbook
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                    var loop = true;
                    int i    = 0;
                    while (loop)
                    {
                        i++;

                        // get first cell, being termname
                        var cell1 = worksheet.Cells[i, 1].Value.SafeTrim();

                        if (cell1.IsNull())
                        {
                            loop = false;
                        }
                        else if (i == 1)
                        {
                            // header row, skip
                        }
                        else
                        {
                            var termid            = Guid.Parse(worksheet.Cells[i, 2].Value.SafeTrim());
                            var termname          = worksheet.Cells[i, 5].Value.SafeTrim();
                            var descr             = worksheet.Cells[i, 6].Value.SafeTrim();
                            var isavailfortagging = GenUtil.SafeToBool(worksheet.Cells[i, 7].Value);

                            var labels = new List <string>();
                            int j      = 10;
                            while (true)
                            {
                                var cellj = worksheet.Cells[i, j].Value.SafeTrim();

                                if (cellj.IsNull())
                                {
                                    break;
                                }
                                else
                                {
                                    labels.Add(cellj);
                                }

                                j++;
                            }

                            // remove termname from label set, and return distinct only
                            labels.RemoveAll(x => x.Trim().ToLower() == termname.Trim().ToLower());
                            labels = labels.Distinct().ToList();

                            lstObjs.Add(new SimpleImportObj()
                            {
                                descr             = descr,
                                isAvailForTagging = isavailfortagging,
                                termId            = termid,
                                termName          = termname,
                                labels            = labels
                            });
                        }
                    } // while
                }     // using
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }

            return(msg == "");
        }