<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Create PortalItem with zip file in .NET Maps SDK Questions</title>
    <link>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223363#M11432</link>
    <description>&lt;P&gt;We are trying to upload zip files to AGOL.&amp;nbsp; If I was doing in AGOL I would create the item, define it as a Code Sample (which will allow a generic zip file).&lt;/P&gt;&lt;P&gt;Hoped we could do this in Runtime (100.15).&amp;nbsp; I see this old thread&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-runtime-sdk-for-net-questions/how-to-create-a-new-featureservice-portalitem/td-p/194325" target="_self"&gt;How to create a new FeatureService PortalItem&lt;/A&gt;&amp;nbsp;which seems to indicate only saving Maps is supported, but it is 3 years old so would hope that is no longer the case.&lt;/P&gt;&lt;P&gt;Seems pretty simple:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;ZipFile.CreateFromDirectory(projectItem.MapPackagePath, outputfile);
var portalItem = new PortalItem(portal, PortalItemType.CodeSample, projectItem.Title);

using ( var fileStream = File.OpenRead(outputfile) )
{
	await portalItem.UpdateDataAsync(fileStream);
}

await portal.User.AddPortalItemAsync(portalItem);
await portalItem.ShareWithGroupsAsync(new[] { group });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Couple problems occur.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;It requires that the user re-enter credentials on the call to new PortalItem even though authenticated&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;The primary issue:&lt;/STRONG&gt;&amp;nbsp;it never returns from the &lt;FONT color="#3366FF"&gt;&lt;EM&gt;await portalItem.UpdateDataAsync&lt;/EM&gt;&lt;/FONT&gt;.&amp;nbsp; It just hangs and so cannot ever put the item in AGOL&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Is what I need to do supported?&amp;nbsp; If so what would be a correct way to do this&lt;/P&gt;&lt;P&gt;Thanks - Joe&lt;/P&gt;</description>
    <pubDate>Wed, 19 Oct 2022 16:08:33 GMT</pubDate>
    <dc:creator>JoeHershman</dc:creator>
    <dc:date>2022-10-19T16:08:33Z</dc:date>
    <item>
      <title>Create PortalItem with zip file</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223363#M11432</link>
      <description>&lt;P&gt;We are trying to upload zip files to AGOL.&amp;nbsp; If I was doing in AGOL I would create the item, define it as a Code Sample (which will allow a generic zip file).&lt;/P&gt;&lt;P&gt;Hoped we could do this in Runtime (100.15).&amp;nbsp; I see this old thread&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-runtime-sdk-for-net-questions/how-to-create-a-new-featureservice-portalitem/td-p/194325" target="_self"&gt;How to create a new FeatureService PortalItem&lt;/A&gt;&amp;nbsp;which seems to indicate only saving Maps is supported, but it is 3 years old so would hope that is no longer the case.&lt;/P&gt;&lt;P&gt;Seems pretty simple:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;ZipFile.CreateFromDirectory(projectItem.MapPackagePath, outputfile);
var portalItem = new PortalItem(portal, PortalItemType.CodeSample, projectItem.Title);

using ( var fileStream = File.OpenRead(outputfile) )
{
	await portalItem.UpdateDataAsync(fileStream);
}

await portal.User.AddPortalItemAsync(portalItem);
await portalItem.ShareWithGroupsAsync(new[] { group });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Couple problems occur.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;It requires that the user re-enter credentials on the call to new PortalItem even though authenticated&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;The primary issue:&lt;/STRONG&gt;&amp;nbsp;it never returns from the &lt;FONT color="#3366FF"&gt;&lt;EM&gt;await portalItem.UpdateDataAsync&lt;/EM&gt;&lt;/FONT&gt;.&amp;nbsp; It just hangs and so cannot ever put the item in AGOL&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Is what I need to do supported?&amp;nbsp; If so what would be a correct way to do this&lt;/P&gt;&lt;P&gt;Thanks - Joe&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 16:08:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223363#M11432</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2022-10-19T16:08:33Z</dc:date>
    </item>
    <item>
      <title>Re: Create PortalItem with zip file</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223555#M11433</link>
      <description>&lt;P&gt;I believe you can call update only on an existing portal item so I suggest :&lt;/P&gt;&lt;P&gt;- Add PortalItem first then call update&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;- Enclose UpdateItemAsync() in try catch block to catch any errors.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 20:02:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223555#M11433</guid>
      <dc:creator>PreetiMaske</dc:creator>
      <dc:date>2022-10-19T20:02:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create PortalItem with zip file</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223559#M11434</link>
      <description>&lt;P&gt;No exceptions are being thrown, it just does not return from the method call&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 20:08:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223559#M11434</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2022-10-19T20:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create PortalItem with zip file</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223987#M11440</link>
      <description>&lt;P&gt;Never found a way to get anything to work with the Runtime API, but was able to do using the rest endpoint directly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;string zipfileName = CreateProjectZipfile(projectItem);

/* Does not seem that Runtime API handles creating a zip file item, so using rest API directly
/*    https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm */

string url = $"https://www.arcgis.com/sharing/rest/content/users/{portal.User?.UserName}/additem";

using ( var formContent = new MultipartFormDataContent() )
{
	AddStringContent(formContent, projectItem);
	AddFileContent(formContent, zipfileName, projectItem);

	using ( HttpClient client = new HttpClient(new ArcGISHttpClientHandler()) )
	{
		var response = await client.PostAsync(url, formContent);
		var json = await response.Content.ReadAsStringAsync();

		await ShareItemWithGroupAsync(json);
	}
}


private void AddStringContent(MultipartFormDataContent formContent, ProjectItem projectItem)
{
	Dictionary&amp;lt;string, string&amp;gt; parameters = new Dictionary&amp;lt;string, string&amp;gt;()
	{
		{ "multipart", "false" },
		{ "title", projectItem.Title },
		{ "type", "Code Sample" },
		{ "async", "false" },
		{ "f", "json" }
	};

	//  For MultipartFormDataContent paramaters needs to be added as individual StringContent objects.  FormUrlEncodedContent does not work.
	foreach (var keyValuePair in parameters)
	{
		formContent.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
	}
}

private void AddFileContent(MultipartFormDataContent formContent, string outputfile, ProjectItem projectItem)
{
	var byteArrayContent = new ByteArrayContent(File.ReadAllBytes(outputfile));

	byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-zip-compressed");
	
	byteArrayContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("form-data");
	byteArrayContent.Headers.ContentDisposition.FileName = $"{projectItem.Title}.zip";
	
	// ContentDisposition.Name needs to match the parameter name
	byteArrayContent.Headers.ContentDisposition.Name = "file";

	formContent.Add(byteArrayContent);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 19:12:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/create-portalitem-with-zip-file/m-p/1223987#M11440</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2022-10-20T19:12:35Z</dc:date>
    </item>
  </channel>
</rss>

