Exemplo n.º 1
0
        public static void PublishFileToLevel(this File file, FileLevel level)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            var publishingRequired = false;
            var approvalRequired = false;

            if (level == FileLevel.Draft || level == FileLevel.Published)
            {
                var context = file.Context;
                var parentList = file.ListItemAllFields.ParentList;
                context.Load(parentList,
                            l => l.EnableMinorVersions,
                            l => l.EnableModeration,
                            l => l.ForceCheckout);

                var checkOutRequired = false;

                try
                {
                    context.ExecuteQueryRetry();
                    checkOutRequired = parentList.ForceCheckout;
                    publishingRequired = parentList.EnableMinorVersions; // minor versions implies that the file must be published
                    approvalRequired = parentList.EnableModeration;
                }
                catch (ServerException ex)
                {
                    if (ex.Message != "The object specified does not belong to a list.")
                    {
                        throw;
                    }
                }

                if (file.CheckOutType != CheckOutType.None || checkOutRequired)
                {
                    Log.Debug(Constants.LOGGING_SOURCE, "Checking in file '{0}'", file.Name);
                    file.CheckIn("Checked in by provisioning", publishingRequired ? CheckinType.MinorCheckIn : CheckinType.MajorCheckIn);
                    context.ExecuteQueryRetry();
                }

                if (level == FileLevel.Published)
                {
                    if (publishingRequired)
                    {
                        Log.Debug(Constants.LOGGING_SOURCE, "Publishing file '{0}'", file.Name);
                        file.Publish("Published by provisioning");
                        context.ExecuteQueryRetry();
                    }

                    if (approvalRequired)
                    {
                        Log.Debug(Constants.LOGGING_SOURCE, "Approving file '{0}'", file.Name);
                        file.Approve("Approved by provisioning");
                        context.ExecuteQueryRetry();
                    }
                }
            }
        }
        public static void PublishFileToLevel(this File file, FileLevel level)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            var publishingRequired = false;
            var approvalRequired = false;

            if (level == FileLevel.Draft || level == FileLevel.Published)
            {
                var context = file.Context;

                bool normalFile = true;
                var checkOutRequired = false;
                if (normalFile)
                {
                    var parentList = file.ListItemAllFields.ParentList;
                    context.Load(parentList,
                                l => l.EnableMinorVersions,
                                l => l.EnableModeration,
                                l => l.ForceCheckout);

                    try
                    {
                        context.ExecuteQueryRetry();
                        checkOutRequired = parentList.ForceCheckout;
                        publishingRequired = parentList.EnableMinorVersions; // minor versions implies that the file must be published
                        approvalRequired = parentList.EnableModeration;
                    }
                    catch (ServerException ex)
                    {
                        if (ex.Message != "The object specified does not belong to a list.")
                        {
                            if (ex.Message.StartsWith("Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null.") &&
                                ex.Message.Contains("ListItemAllFields"))
                            {
                                // E.g. custom display form aspx page being uploaded to the libraries Forms folder
                                normalFile = false;
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }

                if (file.CheckOutType != CheckOutType.None || checkOutRequired)
                {
                    Log.Debug(Constants.LOGGING_SOURCE, "Checking in file '{0}'", file.Name);
                    file.CheckIn("Checked in by provisioning", publishingRequired ? CheckinType.MinorCheckIn : CheckinType.MajorCheckIn);
                    context.ExecuteQueryRetry();
                }

                if (level == FileLevel.Published)
                {
                    if (publishingRequired)
                    {
                        Log.Debug(Constants.LOGGING_SOURCE, "Publishing file '{0}'", file.Name);
                        file.Publish("Published by provisioning");
                        context.ExecuteQueryRetry();
                    }

                    if (approvalRequired)
                    {
                        Log.Debug(Constants.LOGGING_SOURCE, "Approving file '{0}'", file.Name);
                        file.Approve("Approved by provisioning");
                        context.ExecuteQueryRetry();
                    }
                }
            }
        }