Wednesday, January 11, 2012

Properties or AllProperties of SPWeb / SPWebApplication

Properties and Properties Bag may be differently if you are working on SharePoint. Especially for to update and remove data from their collection.



If you want to add

webApp.Properties.Add(p1);

That means you just add a propery to Web Application's properties and SPWeb's properties bag. However, if you remove this key out properties list, like following the code:

webApp.Properties.Remove(p1);

That means you just remove p1 out from SPWeb's properties bag, and in Web Application's properties is still existed. So, at the next time, you will never add or after sucessfull adding, nothing is added into SPWeb.

So, the best practice is always update or remove from both properties to be sured the system works correctly.


Add Properties


web.AllowUnsafeUpdates = true;


//Add property if not exist

if (web.AllProperties[strPropertyKey] == null)
{
       web.AllProperties.Add(strPropertyKey, strPropertyValue);
}




if (web.Properties[strPropertyKey] == null)
{
       web.Properties.Add(strPropertyKey, strPropertyValue);
}



web.Update();
web.Properties.Update();
web.AllowUnsafeUpdates = false;

Update properties



web.AllowUnsafeUpdates = true;


web.AllProperties[strPropertyKey] =  strPropertyValue;


web.Properties[strPropertyKey] = strPropertyValue;



web.Update();
web.Properties.Update();
web.AllowUnsafeUpdates = false;

Delete Properties


web.AllowUnsafeUpdates = true;


web.Properties.Remove(strPropertyKey);


web.AllProperties[strPropertyKey] =  null;


web.Update();
web.Properties.Update();
web.AllowUnsafeUpdates = false;

Hope this help.

Tuesday, January 3, 2012

How to delete a site with all sub-sites

SharePoint does not allow user delete the site which has many sub-sites existed. That means if there are many sub-sites are still working, and user try to delete the parent site, the error will throw exception. Here is the piece code to help delete the parent site with long operation.



private void DeleteAllWebs()
        {
            SPLongOperation.Begin(delegate(SPLongOperation longOperation)
            {
                try
                {

                    int lCID = base.Web.Locale.LCID;
                    uint language = base.Web.Language;
                    int uIVersion = base.Web.UIVersion;

                    string layouts = Utilities.DetermineLayoutsUrl(base.Web.ParentWeb, this.Context, false, false);

                    this.DeleteWeb(base.Web);

                    string queryString = string.Concat(new string[]
   {
   "c=",
   lCID.ToString(CultureInfo.InvariantCulture),
   "&ui=",
   language.ToString(CultureInfo.InvariantCulture),
   "&uiv=",
   uIVersion.ToString(CultureInfo.InvariantCulture)
   });
                    longOperation.End(layouts + "webdeleted.aspx", SPRedirectFlags.UseSource, this.Context, queryString);


                }
                catch (Exception ex)
                {
                    logger.Error("DeleteCommunity", ex);
                    throw ex;
                }
            }
            );
        }

        protected void DeleteWeb(SPWeb web)
        {
            if (web.Webs.Count > 0)
            {
                foreach (SPWeb subweb in web.Webs)
                {
                    try
                    {
                        DeleteWeb(subweb);
                    }
                    finally
                    {
                        if (subweb != null)
                            subweb.Dispose();
                    }
                }
            }
            if (web.Exists)
                web.Delete();
        }


Hope this help!