Hi,
In my C# application, I am trying to access a Feature service which is hosted on arcgis.com. The feature service is shared only with a group or organization and is NOT public. How can I access this service in my client code. Here is my client code which throws an exception saying the Authentication Token required. I do have the token but dont how to use that token to be successfully able to use the service. This same code runs fine for services that are shared as PUBLIC.
IPropertySet sipPS = new PropertySet();
sipPS.SetProperty("DATABASE", layerUrl);
IWorkspaceFactory sipWSF = (IWorkspaceFactory)new ESRI.ArcGIS.Carto.FeatureServiceWorkspaceFactory();
IWorkspace sipWS = sipWSF.Open(sipPS, 0);
IFeatureWorkspace sipFWS = (IFeatureWorkspace)sipWS;
IGroupLayer grpLayer = new GroupLayerClass();
grpLayer.Name = Constants.ArcGISOnlineGroupLayerName;
IFeatureClass sipFC = sipFWS.OpenFeatureClass("0");
IFeatureLayer fl = new FeatureLayerClass();
fl.Name = featureLayerName;
fl.FeatureClass = sipFC;
grpLayer.Add(fl as ILayer);
This doesn't answer Wasim's authentication question but I am grateful for his code sample. I was able to use it to connect to our homegrown ArcGIS feature services to create an IFeatureClass that I can work with in my ArcMap Add-In. Here is my VB .NET code:
Public Function BA_OpenFeatureClassFromService(ByVal url As String, ByVal layerId As Integer) As IFeatureClass
Dim sipPs As IPropertySet = New PropertySet()
Dim sipWSF As IWorkspaceFactory = New FeatureServiceWorkspaceFactory()
Dim sipWS As IWorkspace = Nothing
Dim sipFws As IFeatureWorkspace = Nothing
Try
'Trim any data after "FeatureServer" in url
Dim idxFs As Integer = url.IndexOf("FeatureServer")
url = url.Substring(0, (idxFs + "FeatureServer".Length))
sipPs.SetProperty("DATABASE", url)
sipWS = sipWSF.Open(sipPs, 0)
sipFws = CType(sipWS, IFeatureWorkspace)
Dim strLayerId As String = CType(layerId, String)
Return sipFws.OpenFeatureClass(strLayerId)
Catch ex As Exception
Debug.Print("BA_OpenFeatureClassFromService Exception: " & ex.Message)
Return Nothing
Finally
sipPs = Nothing
sipWS = Nothing
sipFws = Nothing
GC.WaitForPendingFinalizers()
GC.Collect()
End Try
End Function