by
7,382 32
0
7
2,666
0
Top 1% !
Popular
Famous
Tagged
Specified
Refined
OpenSource
Popularity: 6399th place
Created
Modified Sep 18, 2013

Published on:

.NETFramework4.0
Architecturex86_64
Extension.cs
InputName0currentUser
InputName1title
InputName2descr
InputName3code
InputType0User
InputType1string
InputType2string
InputType3string
LanguageCSharp
LicenseMIT_X11
MethodNameCreateSnippet
OSWindows
OutputTypevoid
Windows VersionWindows7

Snip2Code APIs: programmatically add a snippet

This is the fastest way to add a snippet to your bucket in Snip2Code.
Be sure to be logged in before sending such request.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/11207/Snip2Code-APIs--programmatically-add-a-s?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public void CreateSnippet(User currentUser, string title, string descr, string code) { if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(code)) return; //save this snippet in the basket of the user: Snippet newSnip = new Snippet(); newSnip.Name = title; if (!string.IsNullOrWhiteSpace(descr)) newSnip.Description = descr; newSnip.Code = code; newSnip.TargetGroupID = currentUser.PersonalGroupID; newSnip.Relevance = 10; //optional fields: newSnip.Tags = new string[]{"tag1", "tag2", "tag3"}; newSnip.Properties = new SnippetProperty[]{new SnippetProperty("OS", "Windows"), new SnippetProperty("Language", "C#")}; SaveSnippet(newSnip); } public long SaveSnippet(Snippet snippet) { if ((snippet == null) || (snippet.AreFieldsValid() != SnippetWrongField.OK)) { log.ErrorFormat("Input error: snippet={0}, creator={1}", snippet.PrintNull(), CurrentUser.PrintNull()); return -1; } //send the request and parse the response: string contentToSend = "content=" + HttpUtility.UrlEncode(Serialize(snippet)); //you can use a third party library to pass from objecto to json-encoded request... string CREATE_SNIPPET_URL = "http://www.snip2code.com/Snippets/Add"; string response = SendRequest(CREATE_SNIPPET_URL, contentToSend, true); S2CResObj<object> resp = Deserialize(response); //you can use a third party library to pass from json-encoded response to the object... //build the result: long result = -1; if (resp != null) { if (!long.TryParse(resp.Data.ToString(), out result)) { log.ErrorFormat("Invalid response: {0}", resp.Data.ToString()); return -1; } } return result; }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus