Frage

Mit SharePoint 2010 versuche ich, die SPWebConFigModifications -Klasse zu verwenden, um einige grundlegende Änderungen an Web.config -Dateien auf Anwendungen in der Farm vorzunehmen, einschließlich der zentralen Verwaltungsdatei. Modifikationen:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
        String ownerId = properties.Feature.DefinitionId.ToString();
        List<SPWebConfigModification> modsToAdd = new List<SPWebConfigModification>();

        #region Authentication Providers

        modsToAdd.Add(new SPWebConfigModification()
        {
            Name = "defaultProvider",
            Owner = ownerId,
            Path = "configuration/system.web/membership",
            Sequence = 0,
            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute,
            Value = "FBA_AD_MP"
        });

        modsToAdd.Add(new SPWebConfigModification()
        {
            Name = "add [@name=\"FBA_AD_MP\"] [@type=\"System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"] [@connectionStringName=\"ADFBAConnectionString\"] [@enableSearchMethods=\"true\"] [@attributeMapUsername=\"userPrincipalName\"]",
            Owner = ownerId,
            Path = "configuration/system.web/membership/providers",
            Sequence = 0,
            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
            Value = "<add name=\"FBA_AD_MP\" type=\"System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" connectionStringName=\"ADFBAConnectionString\" enableSearchMethods=\"true\" attributeMapUsername=\"userPrincipalName\" />"
        });

        #endregion

        #region Connection Strings

        modsToAdd.Add(new SPWebConfigModification()
        {
            Name = "connectionStrings",
            Owner = ownerId,
            Path = "configuration",
            Sequence = 0,
            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection,
            Value = "<connectionStrings />"
        });

        modsToAdd.Add(WebConfigModificationsUtility.CreateConnectionStringModification(ownerId, 1, "ADFBAConnectionString", properties.Feature.Properties["ADFBAConnectionString"].Value));

        #endregion

        WebConfigModificationsUtility.AddWebConfigModifications(webApp, modsToAdd.ToArray());
    }

Die WebConfigModificationsUtility -Klasse ist ein einfaches Dienstprogramm, das von diesen Feature -Empfängern geteilt wird:

public static void AddWebConfigModifications(SPWebApplication webApp, params SPWebConfigModification[] modificationsToAdd)
    {
        AddWebConfigModifications(webApp, true, modificationsToAdd);
    }
public static void AddWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToAdd)
    {
        foreach (SPWebConfigModification curMod in modificationsToAdd)
        {
            SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Add(curMod);                
        }

        if (persistChanges)
        {
            PersistWebConfigModifications(webApp);
        }
    }
public static void PersistWebConfigModifications(SPWebApplication webApp)
    {
        SPWebService.ContentService.WebApplications[webApp.Id].Update();                        
        SPWebService.ContentService.ApplyWebConfigModifications();
    }

Bei Inhaltsanwendungen funktioniert alles gut. Wenn Sie jedoch versuchen, Funktionen in der zentralen Administratoranwendung zu aktivieren, werden die SPWebconFigModificaiton -Elemente nie in die Web.config -Dateien geschrieben. Ich habe überprüft, ob der Code ohne Ausnahmen ausgeführt wird. Darüber hinaus versucht mein Feature -Empfänger, Änderungen zu entfernen, bei der Deaktivierung vorgenommen:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            String ownerId = properties.Feature.DefinitionId.ToString();
            WebConfigModificationsUtility.RemoveWebConfigModifications(webApp, ownerId);
        }
public static void RemoveWebConfigModifications(SPWebApplication webApp, String ownerId, Boolean persistChanges)
        {            
            RemoveWebConfigModifications(webApp, persistChanges, webApp.WebConfigModifications.Where(x => x.Owner == ownerId).ToArray());
        }
public static void RemoveWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToRemove)
        {
            foreach (SPWebConfigModification curMod in modificationsToRemove)
            {
                SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Remove(curMod);                
            }

            if (persistChanges)
            {
                PersistWebConfigModifications(webApp);
            }
        }

Wenn dieser Code auf der zentralen Administrator -Webanwendung ausgeführt wird, findet er die vier Änderungen, die in der Funktionsaktivierung erstellt wurden, und entfernt sie erfolgreich, aber aber weder Von diesen Vorgängen ändert sich tatsächlich die Web.config -Datei der Webanwendung. Es ist nicht einmal berührt, der Zeitstempel für das letzte bearbeitete Datum bleibt überall gleich.

Ich habe verschiedene Blogs gefunden, die über die Erbe -Schwarzheit der SPWebconfigModification -Klasse sprechen. alle empfohlenen Praktiken. Ich fange an zu vermuten, dass es einen Fehler in der API gibt, aber ich frage mich, ob jemand dies in SharePoint 2010 erfolgreich getan hat oder ob ich offene Reflektor knacken und versuchen sollte zu sehen, was gibt?

War es hilfreich?

Lösung 2

Ich habe gerade eine Benachrichtigung über den Stack -Überlauf erhalten, dass ich ein Abzeichen für diese Frage erhalten habe, basierend auf Ansichten, und ich erinnerte mich nicht einmal daran, diese Frage auf Spooverflow gestellt zu haben, weil es zu der Zeit nicht wirklich existierte.

Der Punkt ist, ich habe diese Frage auch zu so gestellt und dort eine korrekte Antwort erhalten:

"Genau wie ContentService verfügt SPWebservice über ein Administrationsservice -Objekt, das auch über eine Webanapplikationssammlung verfügt. Verwenden Sie den Verwaltungsservice, um zu Ihren CentralAdmin -Webanwendungen zu gelangen."

https://stackoverflow.com/a/3919161/21418

Andere Tipps

Ich gehe davon aus, dass die Funktion Webanwendungen abgebildet ist.

Aktivieren Sie die Funktion über den Browser (Central Admin)?

Versuchen Sie es stattdessen von PowerShell/Stsadm aktivieren. Ich habe immer festgestellt, dass wenn ich versuche, das Web.config aus dem Prozess, der das Web.config verwendet, aus dem Prozess zu ändern, scheinen die Dinge schlecht zu werden.

function New-WebConfigMod {
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)][Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplication,
        [string]$path,
        [string]$name,
        [string]$owner,
        [string]$value,         
        [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]$type=[Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureChildNode
    )
    process {       
        $WebApp = $WebApplication.Read()

        $mod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
        $mod.Path = $path;
        $mod.Name = $name;
        $mod.Sequence = 0;
        $mod.Owner = $owner;
        $mod.Type = $type;
        $mod.Value = $value;        
        $WebApp.WebConfigModifications.Add($mod) 
        $WebApp.Update() 
        $WebApp.WebService.ApplyWebConfigModifications();

        $WebApplication
    } 
} 

Get-SPWebApplication -IncludeCentralAdministration |? {
        $_.Url -eq "http://xyz:8080/"
    } | New-WebConfigMod -path "configuration" -name "connectionStrings" -owner "connectionStrings" -value "<connectionStrings></connectionStrings>" | 
        out-null

Oder konvertieren Sie das in Ihre Funktion

webApp.WebConfigModifications.Add(curMod);
webApp.Update();
webApp.WebService.ApplyWebConfigModifications();  

Nicht wirklich sicher, aber versuchen Sie diesen Code zu verwenden ...

    public static void AddWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToAdd)    
    {
    foreach (SPWebConfigModification curMod in modificationsToAdd)
        {          
        webApp.WebConfigModifications.Add(curMod);
        webApp.Update();
        webApp.Farm.Services.GetValue<spwebservice></spwebservice>().ApplyWebConfigModifications();  
    }
.
.
.
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top