private void InitializeApplication()
        {
            UrlQuery query = new UrlQuery(this.Page.Request.Url.ToString());

            ForumApplication.Instance.BasePath          = SPEncode.UrlEncodeAsUrl(query.Url);
            ForumApplication.Instance.Title             = this.Name;
            ForumApplication.Instance.ForumCache        = this.Page.Cache;
            ForumApplication.Instance.ClassResourcePath = this.ClassResourcePath;
            ForumApplication.Instance.SpUser            = SPControl.GetContextWeb(Context).CurrentUser;
            ForumApplication.Instance.AppPoolUser       = SPControl.GetContextSite(Context).OpenWeb().CurrentUser;
            ForumApplication.Instance.SpWeb             = SPControl.GetContextSite(Context).OpenWeb();
        }
示例#2
0
        /// <summary>
        /// Relative url for zoom builder
        /// </summary>
        /// <returns>The relative Url</returns>
        private static string ZoomBuilderRelativeUrl()
        {
            HttpContext current  = HttpContext.Current;
            SPWeb       web      = SPControl.GetContextWeb(current);
            string      relative = web.ServerRelativeUrl;

            if (relative == "/")
            {
                relative = "/_layouts/zoombldr.aspx";
            }
            else
            {
                relative += "/_layouts/zoombldr.aspx";
            }

            return(SPEncode.UrlEncodeAsUrl(relative));
        }
示例#3
0
        /// <summary>Copy FileMetadata.</summary>
        /// <param name="fromFileUrl">The from file url.</param>
        /// <param name="toFileUrl">The to file url.</param>
        /// <param name="omitFields">The omit fields.</param>
        /// <param name="changeVersion">The change version.</param>
        /// <returns>The copy file metadata.</returns>
        public string CopyFileMetadata(string fromFileUrl, string toFileUrl, string omitFields, bool changeVersion)
        {
            string response = string.Empty;

            SPSite iSite = null;
            SPWeb  iWeb  = null;
            SPSite oSite = null;
            SPWeb  oWeb  = null;

            try
            {
                iSite = new SPSite(fromFileUrl);
                iWeb  = iSite.RootWeb;
                oSite = new SPSite(toFileUrl);
                oWeb  = oSite.RootWeb;

                // try and obtain valid instances of the SPFile objects at the specified URLs
                SPFile iFile = this.GetFileObject(iSite, fromFileUrl);
                SPFile oFile = this.GetFileObject(oSite, toFileUrl);

                if (iFile == null)
                {
                    throw new FileNotFoundException("unable to find file " + fromFileUrl);
                }
                if (oFile == null)
                {
                    throw new FileNotFoundException("unable to find file " + toFileUrl);
                }

                SPListItem sourceItem = iFile.Item;
                SPListItem targetItem = oFile.Item;

                if (sourceItem == null)
                {
                    MTMLogger.High(this.stERR + "CopyFileMetadata :: File " + fromFileUrl + " has no corresponding item.");
                    return(this.stERR + "CopyFileMetadata :: File " + fromFileUrl + " has no corresponding item.");
                }
                if (targetItem == null)
                {
                    MTMLogger.High(this.stERR + "CopyFileMetadata :: File " + toFileUrl + " has no corresponding item.");
                    return(this.stERR + "CopyFileMetadata :: File " + toFileUrl + " has no corresponding item.");
                }

                string[] blackList = omitFields.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                // trim whitespace from both ends of the elements
                for (int i = 0; i < blackList.Length; i++)
                {
                    blackList[i] = blackList[i].Trim();
                }

                // disable the security validation (temporarily)
                oWeb.AllowUnsafeUpdates = true;

                SPField sourceField = null;
                foreach (SPField targetField in targetItem.Fields)
                {
                    if (((IList)blackList).Contains(targetField.Title) ||
                        this.FieldIsCopyTarget(targetField, sourceItem, ref sourceField) == false)
                    {
                        continue;
                    }

                    try
                    {
                        string sourceValue = Convert.ToString(sourceItem[sourceField.Id]);

                        // TODO: populating the choice-type column has to be separate!
                        if (this.IsTargetSPFieldTypeTextOrChoice(targetField))
                        {
                            this.MatchSPFieldTextOrChoice(targetItem, targetField, iWeb, sourceField, sourceValue);
                        }
                        else if (this.IsTargetSPFieldTypeNumber(targetField))
                        {
                            this.MatchSPFieldTypeNumber(targetItem, targetField, sourceValue);
                        }
                        else if (this.IsSPFieldTypeUserIdem(targetField, sourceField))
                        {
                            this.MatchSPFieldTypeUserIdem(iWeb, oWeb, targetItem, targetField, sourceValue);
                        }
                        else if (this.IsTargetSPFieldTypeUser(targetField))
                        {
                            this.MatchSPFieldTypeUser(oWeb, targetItem, targetField, sourceValue);
                        }
                        else if (this.IsTargetSPFieldTypeLookup(targetField))
                        {
                            this.MatchSPFieldTypeLookup(targetItem, targetField, sourceItem, sourceField, sourceValue);
                        }
                        else if (this.IsTargetSPFieldTypeDateTime(targetField))
                        {
                            this.MatchSPFieldTypeDateTime(targetItem, targetField, sourceValue);
                        }
                        else if (this.IsTargetFieldTaxonomyField(targetField))
                        {
                            this.MatchTaxonomyField(targetItem, targetField, sourceValue);
                        }
                        else
                        {
                            this.MatchSPFieldDefaultId(targetItem, targetField, sourceItem, sourceField);
                        }
                    }
                    catch (Exception ex)
                    {
                        // catch the field update exception and move on
                        response += "Warning: [" + targetField.Title + "] " + ex.Message + '\n';
                    }
                }

                // Set "Document Source" metadata to the source file's URL
                if (targetItem.Fields.ContainsField("Document Source") && !((IList)blackList).Contains("Document Source"))
                {
                    SPField field = targetItem.Fields["Document Source"];

                    // this is a hyperlink-type column
                    if (field.Type == SPFieldType.URL)
                    {
                        SPFieldUrlValue newLink = new SPFieldUrlValue();
                        newLink.Description           = "Link to: " + sourceItem.Name;
                        newLink.Url                   = SPEncode.UrlEncodeAsUrl(iWeb.Url + '/' + sourceItem.Url);
                        targetItem["Document Source"] = newLink;
                    }
                    else
                    {
                        // or just a "single line of text" column
                        targetItem["Document Source"] = SPEncode.UrlEncodeAsUrl(iWeb.Url + '/' + sourceItem.Url);
                    }
                }

                // apply metadata update without triggering any events
                using (new DisabledItemEventsScope())
                {
                    // don't create a new version; the item is updated regardless of checkout status
                    if (changeVersion == false)
                    {
                        try
                        {
                            targetItem.SystemUpdate(false);
                        }
                        catch (Exception ex)
                        {
                            MTMLogger.High(this.stERR + "CopyFileMetadata :: " + ex.Message);
                            response = this.stERR + "CopyFileMetadata :: " + ex.Message;
                        }
                    }

                    // follow proper checkout-update-checkin procedure
                    else
                    {
                        // check if the document is checked in
                        // (depends on the security settings of the document library)
                        if (oFile.CheckOutType == SPFile.SPCheckOutType.None)
                        {
                            try
                            {
                                oFile.CheckOut();
                                // Update the item
                                targetItem.Update();
                                oFile.CheckIn(string.Empty);
                            }
                            catch (Exception ex)
                            {
                                MTMLogger.High(stERR + "CopyFileMetadata :: " + ex.Message);
                                response = stERR + "CopyFileMetadata :: " + ex.Message;
                            }
                        }
                        else
                        {
                            try
                            {
                                // Update the item
                                targetItem.Update();
                            }
                            catch (Exception ex)
                            {
                                MTMLogger.High(stERR + "CopyFileMetadata :: " + ex.Message);
                                response = stERR + "CopyFileMetadata :: " + ex.Message;
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                MTMLogger.High(stERR + "CopyFileMetadata :: " + ex.Message);
                response = stERR + "CopyFileMetadata :: " + ex.Message;
            }
            catch (Exception ex)
            {
                MTMLogger.High(stERR + "CopyFileMetadata :: " + ex.Message);
                response = stERR + "CopyFileMetadata :: " + ex.Message;
            }
            finally
            {
                if (iWeb != null)
                {
                    iWeb.Dispose();
                }
                if (iSite != null)
                {
                    iSite.Dispose();
                }
                if (oWeb != null)
                {
                    oWeb.Dispose();
                }
                if (oSite != null)
                {
                    oSite.Dispose();
                }
            }

            return(response);
        }
        /// <summary>
        /// Does the paste action
        /// </summary>
        private void PastePicsHere()
        {
            // Pro
            // SiteUrl
            // PictureLibrary ID
            // FileUrl
            Debug.WriteLine("** _eventArgs: " + _eventArgs);
            string[] pasteData = _eventArgs.Split('|');

            SPWeb  currWeb = SPContext.Current.Web;
            SPList list    = SPContext.Current.List;


            // Rootfolder contains the current folder
            string destinationFolderUrl = Page.Request.QueryString["RootFolder"];

            // Check for valid url
            // Default views (aka root folders) don´t contain a RootFolder QueryString
            if (string.IsNullOrEmpty(destinationFolderUrl))
            {
                //destinationFolderUrl = currWeb.ServerRelativeUrl + "/" + SPEncode.UrlEncodeAsUrl(list.Title);
                destinationFolderUrl = currWeb.Url + "/" +
                                       SPEncode.UrlEncodeAsUrl(NormalizeUrl(list.RootFolder.ToString()));
                // list.Title
            }

            try
            {
                // Open source site and to get the file or folder
                using (SPSite site = new SPSite(pasteData[1]))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        // Folder object from url
                        // Reopen too get elevated privileges
                        // SPWeb destinationWeb = site.OpenWeb(currWeb.ID);
                        Debug.WriteLine("Destination Folder:" + destinationFolderUrl);
                        SPFolder destinationFolder = currWeb.GetFolder(destinationFolderUrl);

                        Debug.WriteLine("Destination Folder:" + destinationFolderUrl);
                        Debug.WriteLine("Source Site:" + pasteData[1]);
                        Debug.WriteLine("Source List ID:" + pasteData[2]);
                        Debug.WriteLine("Source List ID:" + pasteData[3]);
                        Debug.WriteLine("Source Items IDs:" + pasteData[4]);

                        string[] pictureIds = pasteData[4].Split(';');
                        SPList   sourceList = web.Lists[new Guid(pasteData[2])];

                        // Paste the file

                        foreach (string id in pictureIds)
                        {
                            SPListItem listItem = sourceList.GetItemById(Int32.Parse(id));

                            string itemFolder = web.ServerRelativeUrl + "/" +
                                                listItem.Url.Substring(0,
                                                                       listItem.Url.LastIndexOf('/'));

                            Debug.WriteLine(string.Format("Source Folder {0} - {1}", itemFolder, pasteData[3]));

                            if (itemFolder != SPEncode.HtmlDecode(pasteData[3]))
                            {
                                Guid   guidFile = listItem.File.UniqueId;
                                SPFile file     = web.GetFile(guidFile);

                                Debug.WriteLine("** Coping File");
                                CopyFile(file, destinationFolder);

                                //Delete the file if the action is "Cut"
                                if (pasteData[0].ToLower() == "ProCutPic".ToLower())
                                {
                                    //file.Delete();
                                    file.Recycle();
                                }
                            }
                        }
                    }
                }

                RefreshPage();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        /// <summary>
        /// Does the paste action
        /// </summary>
        private void PasteHere()
        {
            // Pro
            // SiteUrl
            // FileUrl
            Debug.WriteLine("** _eventArgs: " + _eventArgs);
            string[] pasteData = _eventArgs.Split('|');
            SPSite   site;
            SPWeb    web;

            SPWeb  currWeb = SPContext.Current.Web;
            SPList list    = SPContext.Current.List;

            // Rootfolder contains the current folder
            string destinationFolderUrl = Page.Request.QueryString["RootFolder"];

            // Check for valid url
            // Default views (aka root folders) don´t contain a RootFolder QueryString
            if (string.IsNullOrEmpty(destinationFolderUrl))
            {
                destinationFolderUrl = currWeb.Url + "/" + SPEncode.UrlEncodeAsUrl(
                    NormalizeUrl(list.RootFolder.ToString()));
                // list.Title
            }

            try
            {
                // Open source site and to get the file or folder
                using (site = new SPSite(pasteData[1]))
                {
                    web = site.OpenWeb();

                    // Folder object from url
                    // Reopen too get elevated privileges
                    SPFolder destinationFolder = currWeb.GetFolder(destinationFolderUrl);

                    Debug.WriteLine("Destination Folder:" + destinationFolderUrl);
                    Debug.WriteLine("Source Site:" + pasteData[1]);
                    Debug.WriteLine("Source File-Folder:" + pasteData[2]);

                    // Paste the file
                    object fileOrFolder = web.GetFileOrFolderObject(pasteData[2]);

                    if (fileOrFolder is SPFile)
                    {
                        Debug.WriteLine("** Coping File");
                        SPFile file = fileOrFolder as SPFile;
                        CopyFile(file, destinationFolder);
                    }
                    else if (fileOrFolder is SPFolder)
                    {
                        Debug.WriteLine("** Coping Folder");
                        SPFolder folder = fileOrFolder as SPFolder;
                        CopyFolder(folder, destinationFolder);
                    }

                    //Delete the file if the action is "Cut"
                    if (pasteData[0].ToLower() == "ProCut".ToLower())
                    {
                        if (fileOrFolder is SPFile)
                        {
                            Debug.WriteLine("** Deleting File");
                            SPFile file = fileOrFolder as SPFile;
                            //file.Delete();
                            file.Recycle();
                        }
                        else if (fileOrFolder is SPFolder)
                        {
                            Debug.WriteLine("** Deleting Folder");
                            SPFolder folder = fileOrFolder as SPFolder;
                            folder.ParentWeb.AllowUnsafeUpdates = true;
                            //folder.Delete();
                            folder.Recycle();
                            folder.ParentWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }

                RefreshPage();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }