Codeausschnitt: Ausführen einer Updatemethodeninstanz eines externen Inhaltstyps
Letzte Änderung: Donnerstag, 6. Mai 2010
Gilt für: SharePoint Server 2010
Inhalt dieses Artikels
Beschreibung
Voraussetzungen
So verwenden Sie dieses Beispiel
Beschreibung
Im folgenden Codebeispiel wird veranschaulicht, wie Sie eine Updater-Methodeninstanz mit einem externen Inhaltstyp unter Verwendung des BDC-Laufzeitobjektmodells auf dem Server programmgesteuert ausführen.
Voraussetzungen
Microsoft SharePoint Server 2010 oder Microsoft SharePoint Foundation 2010 auf dem Server
Microsoft .NET Framework 3.5 auf dem Clientcomputer
Microsoft Visual Studio.
Mindestens ein registrierter externer Inhaltstyp im BDC-Metadatenspeicher
So verwenden Sie dieses Beispiel
Starten Sie Visual Studio, und erstellen Sie ein C#-Konsolenanwendungsprojekt. Stellen Sie sicher, dass Sie beim Erstellen des Projekts .NET Framework 3.5 auswählen.
Klicken Sie im Menü Ansicht auf Eigenschaftenseiten, um die Projekteigenschaften aufzurufen.
Wählen Sie auf der Registerkarte Build unter Zielplattform die Option Beliebige CPU aus.
Schließen Sie das Fenster mit den Projekteigenschaften.
Entfernen Sie im Projektmappen-Explorer unter Verweise sämtliche Projektverweise bis auf System und System.Core.
Fügen Sie dem Projekt die folgenden Verweise hinzu:
Microsoft.BusinessData
Microsoft.SharePoint
System.Web
Ersetzen Sie den automatisch generierten Code in Program.cs durch den Code am Ende dieses Verfahrens.
Ersetzen Sie die Werte von <siteUrl>, nameSpace> und <entityName> durch gültige Werte.
Speichern Sie das Projekt.
Kompilieren Sie das Projekt, und führen Sie es aus.
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
namespace SDKSamples
{
class Methods
{
static void Main(string[] args)
{
BDCUpdate();
}
//How To: Edit an item from an External Content Type
public static void BDCUpdate()
{
//Specify the SiteURL, Namespace and the Entity Name
string SiteURL = "<siteUrl>";
string nameSpace = "<nameSpace>";
string entityName = "<entityName>";
using (SPSite site = new SPSite(SiteURL))
{
using (new Microsoft.SharePoint.SPServiceContextScope(
SPServiceContext.GetContext(site)))
{
BdcService service =
SPFarm.Local.Services.GetValue<BdcService>(
String.Empty);
IMetadataCatalog catalog =
service.GetDatabaseBackedMetadataCatalog(
SPServiceContext.Current);
IEntity entity = catalog.GetEntity(
nameSpace, entityName);
ILobSystemInstance LobSysteminstance =
entity.GetLobSystem().
GetLobSystemInstances()[0].Value;
// Accept the user input for identity value
Console.Write(
"\nEnter identity value for which you want to edit : ");
int identityColumnValue =
int.Parse(Console.ReadLine());
Identity identity =
new Identity(identityColumnValue);
try
{
IEntityInstance ientityinstance =
entity.FindSpecific(
identity, "Read Item", LobSysteminstance);
IFieldCollection fieldCollection =
entity.GetFinderView("Read List").Fields;
//Display the old values
Console.WriteLine("\nOld Values : ");
foreach (IField field in fieldCollection)
{
Console.WriteLine(
field.Name.PadRight(20) + ":" +
ientityinstance[field.Name].ToString());
}
// The following will work only for Sales.Customer table in AdventureWorks2008
// Changing value of "TerritoryID" field
string fieldName = "TerritoryID";
// Example Value for TerritoryID will be 1,4,6 etc.
Console.Write(
"\nEnter the new value for the column {0}: ",
fieldName);
ientityinstance[fieldName] = int.Parse(Console.ReadLine());
ientityinstance["ModifiedDate"] = DateTime.Now;
ientityinstance.Update();
Console.WriteLine("Record updated");
//Display the Updated values
Console.WriteLine("\nUpdated Values : ");
foreach (IField field in fieldCollection)
{
Console.WriteLine(
field.Name.PadRight(20) + ":" +
ientityinstance[field.Name].ToString());
}
}
catch (ObjectNotFoundException exception)
{
Console.WriteLine(
"Identity column with value {0} not found...",
identityColumnValue);
}
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
Siehe auch
Referenz
GetDatabaseBackedMetadataCatalog(SPServiceContext)