Wednesday, January 5, 2011

Access Denied Error Message While Editing Properties of any Document in a MOSS Document Library

Recently after migrating site from one server to other server I face below issue.

You are having administrative access to SharePoint site and you are still getting access denied message. Here's window service code which will ask you for SharePoint site and List Name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;

namespace ResetListFields
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\r\n");
            Console.WriteLine(@"Please Enter WebUrl");
            string WebUrl = Console.ReadLine();
            Console.WriteLine("\r\nPlease Enter ListName");
            string ListName = Console.ReadLine();

            FixField(WebUrl, ListName);
            
        }
        static bool ListsExists(SPWeb CurrentWeb, string CurrentListName)
        {
            try
            {
                SPList List = CurrentWeb.Lists[CurrentListName];
                return true;
            }
            catch
            {
                return false;
            }
        }
        static void FixField(string WebUrl, string ListName)
        {

            string RenderXMLPattenAttribute = "RenderXMLUsingPattern";

            SPSite site;
            try
            {
                site = new SPSite(WebUrl);
            }
            catch (Exception)
            {
                Console.WriteLine("\r\nError while loading site URL. Program will terminate");
                Console.WriteLine("\r\nPress any key to continue...");
                Console.ReadLine();
                return;
            }
            using (SPWeb CurrentWeb = site.OpenWeb())
            {
                if (!ListsExists(CurrentWeb, ListName))
                {
                    Console.WriteLine("\r\nError while loading Custom list. Program will terminate");
                    Console.WriteLine("\r\nPress any key to continue...");
                    Console.ReadLine();
                    return;
                }
            }

            using (SPWeb CurrentWeb = site.OpenWeb())
            {
                SPList CurrentList = null;
                CurrentList = CurrentWeb.Lists[ListName];
                SPField f = CurrentList.Fields.GetFieldByInternalName("PermMask");

                string s = f.SchemaXml;

                Console.WriteLine("\r\nschemaXml before: " + s);

                XmlDocument xd = new XmlDocument();

                xd.LoadXml(s);

                XmlElement xe = xd.DocumentElement;

                if (xe.Attributes[RenderXMLPattenAttribute] == null)
                {

                    XmlAttribute attr = xd.CreateAttribute(RenderXMLPattenAttribute);

                    attr.Value = "TRUE";

                    xe.Attributes.Append(attr);

                }

                string strXml = xe.OuterXml;

                Console.WriteLine("\r\nschemaXml after: " + strXml);

                f.SchemaXml = strXml;
            }

            Console.WriteLine("\r\nPress any key to continue...");
            Console.ReadLine();
            return;

        }
    }
}

I found Anthony Odole post useful.

Hope you will find it useful.

Thanks,
Ashish Chotalia

No comments:

Post a Comment