Tuesday, January 25, 2011

jQuery/Javascript to replace broken images

Many times you find your image links are broken. In firefox, Chrome if your images links are broken then your images are hidden by default. If you see same scenario in IE it will show you 'X' image missing.

I found following snippet quite useful which hides broken images using jquery.



$(document).ready(function(){ 
    $("img").each(function(index) {
        $(this).error(function() {
            $(this).unbind("error").attr("src", "/images/NoImage.png"); // If you want to replace with any blank image.

            $(this).hide();//You can simply Hide it using this.
        });
        $(this).attr("src", $(this).attr("src"));
  });    
});

Hope you find it useful.

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