Gorgo.Live.ToString()

Mariusz, Gorzoch tech Blog

Archive for May 2010

Tweaking up web.config thru SharePoint API

leave a comment »

Each day bring some changes. This is even more true when you are working with SharePoint as the API is so huge and reach that sometimes I do not believe that one person can know and understand all. Today I needed to write a feature which on activation will disable unnecessary HttpModules from web.config. To achieve that you need special class “SPWebConfigModification”. This class let you update selected areas of SharePoint configuration file and spread those changes across your entire farm.

Here are main part of my solution:

features.xml

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="{77490000-6157-4ef0-A70B-6B463BFCFD26}"
         Title="HotFix 01 date 2010-05-25"
         Scope="WebApplication"
         Hidden="FALSE"
         ImageUrl="HotFix.jpg"
         Version="1.0.0.0"
         ReceiverAssembly="Hempel.OneHempel.HotFixes.HF01D20100525, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c4c727ecf8ef0913"
         ReceiverClass="Hempel.OneHempel.HotFixes.HF01D20100525.Code.EventReceiver">
</Feature>

Please notice here what is the scope of this feature. As our solution will tweak web.config, we need to have scope = “WebApplication”.

Second part of this solution is a ReceiverClass which actually is doing tweak up during feature activation:

public override void FeatureActivated(
                SPFeatureReceiverProperties properties)
{
    SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

    SPWebConfigModification modification01 = new SPWebConfigModification("add[@name=’FormsAuthentication’]", "configuration/system.web/httpModules");
    modification01.Owner = ModificationOwner;
    modification01.Sequence = 0;
    modification01.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode ;
    modification01.Value = "<!– <add name=\"FormsAuthentication\" type=\"System.Web.Security.FormsAuthenticationModule\" /> –>";

    SPWebConfigModification modification02 = new SPWebConfigModification("add[@name=’UrlAuthorization’]", "configuration/system.web/httpModules");
    modification02.Owner = ModificationOwner;
    modification02.Sequence = 0;
    modification02.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    modification02.Value = "<!– <add name=\"UrlAuthorization\" type=\"System.Web.Security.UrlAuthorizationModule\" /> –>";

    // Add my new web.config modification.
    webApplication.WebConfigModifications.Add(modification01);
    webApplication.WebConfigModifications.Add(modification02);

    // Save web.config changes.
    webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

    // Serialize the web application state and propagate changes across the farm.
    webApplication.Update();

}

public override void FeatureDeactivating(
                SPFeatureReceiverProperties properties)
{
    SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
    Collection<SPWebConfigModification> collection = webApplication.WebConfigModifications;

    int iStartCount = collection.Count;

    // Remove any modifications that were originally created by the owner.
    for (int c = iStartCount – 1; c >= 0; c–)
    {
        SPWebConfigModification configMod = collection[c];

        if (configMod.Owner == ModificationOwner)
            collection.Remove(configMod);
    }

    // Apply changes only if any items were removed.
    if (iStartCount > collection.Count)
    {
        webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        webApplication.Update();
    }
}

You can find a trick in above code. In all examples I found on the net, class “SPWebConfigModification” is used to update/add attributes. None of those examples didn’t show how to remove section. The trick I found was to replace section with the same section included in comments mark. So this way I manager to write a feature which you can use to disable/remove httpModules : “FormsAuthentication” and “UrlAuthentication”.

have fun playing with that. Here you have excellent post around details : Mark Wagner blog

Advertisements

Written by Mariusz Gorzoch

25 May 2010 at 21:50

Posted in SharePoint

SPPersistedObject – once deployed – DO NOT CHANGE IT

with one comment

and if you will do this, then don’t be surprised that “Sharepoint timer job will stop working” and in addition you will start seeing errors like that in you Sharpeoint “Log” :

High File system cache monitor encoutered error, flushing in memory cache
High The WebUrls field in the Hempel.OneHempel.ArticlePage200.TEMPLATE.Admin.AP200.AP200JobSettings class has been deleted. The old value will be preserved in the UpgradedPersistedFields collection under the WebUrls key. Consider writing upgrade code to copy this value to a new location.
High The ArchiveArticles field in the Hempel.OneHempel.ArticlePage200.TEMPLATE.Admin.AP200.AP200JobSettings class has been deleted. The old value will be preserved in the UpgradedPersistedFields collection under the ArchiveArticles key. Consider writing upgrade code to copy this value to a new location.
High The DeleteArtciles field in the Hempel.OneHempel.ArticlePage200.TEMPLATE.Admin.AP200.AP200JobSettings class has been deleted. The old value will be preserved in the UpgradedPersistedFields collection under the DeleteArtciles key. Consider writing upgrade code to copy this value to a new location.
High Flushing the xml document cache.
High Flushing the xml document cache.
High Flushing the xml document cache.
High File system cache monitor encoutered error, flushing in memory cache
High File system cache monitor encoutered error, flushing in memory cache
High Exception in RefreshCache. Exception message :Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Unexpected The timer service could not initialize its configuration, please check the configuration database. Will retry later.

 

happy I found solution for that… but unfortunately to apply it you need to brake “no one is allowed to touch database” rule!. If you can live with that, then :

  • Stop your SharePoint timer job
  • go to your “SharePoint config db”
  • open “object” table
  • find you persistent objects by “name” (select * from Objects where name ‘’%youObjectClassName%’”
  • delete what you get
  • now, go to the “C:\Documents and Settings\All Users\Application Data\Microsoft\SharePoint\Config\<ContentDBGuid>\ and delete all “.xml” files you find there (be sure to not delete folder as you will get into the problem trying to restore name of it) <- in this step you clearing local cache
  • start your “Sharepoint timer job”

in my case, this bring my timer job to live again. Ufffff

Written by Mariusz Gorzoch

12 May 2010 at 14:21

Posted in Bez kategorii

How to get rid of SPPersistedObject

with one comment

Be aware… SharePoint API is a little bit tricky and here is a good example of that. If you are using “SPPersistedObject” class you should know that to remove this object from persisted store you can not just call “delete” method, but you need also call “Unprovision” and “Uncache”:

AP200JobSettings settings = this.WebAppSelector.CurrentItem.GetChild<AP200JobSettings>(Globals.SharePointAP200JobSettingsId);
//if there is no settings or we get command to clear settings (thru parameter) – then we set-up new empty settings object
if (settings!=null && (!String.IsNullOrEmpty(Request.QueryString["cls"])))
{
    settings.Delete();
    settings.Unprovision();
    settings.Uncache();
    settings = null;
}

if you won’t call the other two methods you will end-up with cryptic error as so:

An update conflict has occurred, and you must re-try this action. The object SPFarm-2b89-4f2e-a928-b52bb8774e3b is being updated by NT AUTHORITY\NETWORK SERVICE, in the w3wp process, on machine OVIDIUBECHES.  View the tracing log for more information about the conflict

Written by Mariusz Gorzoch

12 May 2010 at 11:16

Posted in SharePoint

Hint around Internet Explorer and Javascript

leave a comment »

Lately I was working on one old project build on top of classic ASP. What I found about it was that application was so slow during rendering in client browser. On the end I found that reason for that was JavaScript inline function hooked to “onClick” event. I found that if you have a lot of let say rows generated in table and each row contain “onClick” even, which contain JavaScript with differs literally from JavaScript in other rows then it would take forever to show result to the client:

BAD APPROCHE:

<table>
    <tr onClick=’someFunction(1)’><td>1</td></tr>
    <tr onClick=’someFunction(2)’><td>2</td></tr>
    <tr onClick=’someFunction(3)’><td>3</td></tr>
    <tr onClick=’someFunction(4)’><td>4</td></tr>
    <tr onClick=’someFunction(5)’><td>5</td></tr>
    …
    <tr onClick=’someFunction(9999)’><td>9999</td></tr>
</table>   

GOOD APPROCHE:

<table>
    <tr param="1" onClick=’someFunction(this.param)’><td>1</td></tr>
    <tr param="2" onClick=’someFunction(this.param)’><td>2</td></tr>
    <tr param="3" onClick=’someFunction(this.param)’><td>3</td></tr>
    <tr param="4" onClick=’someFunction(this.param)’><td>4</td></tr>
    <tr param="5" onClick=’someFunction(this.param)’><td>5</td></tr>
    …
    <tr param="9999" onClick=’someFunction(this.param)’><td>9999</td></tr>
</table>   

(please notice that in ‘Good approach’ JavaScript function in onClick event is the same for all rows <- and this is a hint, which speed up rendering)

the final result is the same, but in case of a lot of rows you can find that “GOOD APPROCHE” will take much less time for the browser to show results.

coding can be sometimes tricky and without hints it can lead you to strange results and behavior. Sometimes when ‘I’m looking at “cooking made by my son” I think he can be a good programmer in the future and not because what he get on the end… but rather by the tricks he used to make this think look like a nice pace of chocolate cake.

🙂 happy coding

Written by Mariusz Gorzoch

9 May 2010 at 21:29

Posted in Bez kategorii