/// <summary> /// Executes a query on the server and returns results filtered and expanded /// based on the provided parameters. /// </summary> /// <param name="queryText">Content query text.</param> /// <param name="select">Fields to select.</param> /// <param name="expand">Fields to expand.</param> /// <param name="settings">Query settings.</param> /// <param name="server">Target server.</param> public static async Task <IEnumerable <Content> > QueryAsync(string queryText, string[] select = null, string[] expand = null, QuerySettings settings = null, ServerContext server = null) { if (settings == null) { settings = QuerySettings.Default; } var oreq = new ODataRequest { Path = "/Root", Select = select, Expand = expand, Top = settings.Top, Skip = settings.Skip, SiteUrl = ServerContext.GetUrl(server) }; oreq.Parameters.Add("query", Uri.EscapeDataString(queryText)); if (settings.EnableAutofilters != FilterStatus.Default) { oreq.Parameters.Add("enableautofilters", settings.EnableAutofilters.ToString().ToLower()); } if (settings.EnableLifespanFilter != FilterStatus.Default) { oreq.Parameters.Add("enablelifespanfilter", settings.EnableLifespanFilter.ToString().ToLower()); } return(await Content.LoadCollectionAsync(oreq, server)); }
private static async Task <IEnumerable <Content> > LoadReferencesAsync(string path, int id, string fieldName, string[] select = null, ServerContext server = null) { if (select == null || select.Length == 0) { select = new[] { "*" } } ; var projection = new[] { "Id", "Path", "Type" }; projection = projection.Union(select.Select(p => fieldName + "/" + p)).ToArray(); var oreq = new ODataRequest { SiteUrl = ServerContext.GetUrl(server), Expand = new[] { fieldName }, Select = projection, ContentId = id, Path = path }; dynamic content = await Content.LoadAsync(oreq, server); // we assume that this is an array of content json objects var items = (JArray)content[fieldName]; return(items.Select(c => CreateFromResponse(c, server))); }
/// <summary> /// Loads children of a container. /// </summary> /// <param name="path">Content path.</param> /// <param name="server">Target server.</param> public static async Task <IEnumerable <Content> > GetCollectionAsync(string path, ServerContext server = null) { return(await GetCollectionAsync(new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), Path = path, IsCollectionRequest = true })); }
//============================================================================= Static GET methods /// <summary> /// Loads a content from the server. /// </summary> /// <param name="contentId">Content id.</param> /// <param name="server">Target server.</param> public static async Task <Content> GetContentAsync(int contentId, ServerContext server = null) { return(await GetContentAsync(new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), ContentId = contentId }, server)); }
/// <summary> /// Loads a content from the server. /// </summary> /// <param name="path">Content path.</param> /// <param name="server">Target server.</param> public static async Task <Content> GetContentAsync(string path, ServerContext server = null) { return(await GetContentAsync(new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), Path = path }, server)); }
/// <summary> /// Assembles an http request that gets a stream from the portal containing binary data. /// Use this inside a using block to asynchronously get the response stream. /// Please catch WebExceptions and parse them using the GetClientExceptionAsync method. /// </summary> /// <param name="id">Content id.</param> /// <param name="version">Content version (e.g. V2.3D). If not provided, the highest version /// accessible to the current user will be served.</param> /// <param name="propertyName">Binary field name. Default is Binary.</param> /// <param name="server">Target server.</param> public static HttpWebRequest GetStreamRequest(int id, string version = null, string propertyName = null, ServerContext server = null) { var url = $"{ServerContext.GetUrl(server)}/binaryhandler.ashx?nodeid={id}&propertyname={propertyName ?? "Binary"}"; if (!string.IsNullOrEmpty(version)) { url += "&version=" + version; } return(GetRequest(url, server)); }
/// <summary> /// Uploads a file to the server into the provided container. /// </summary> /// <param name="binaryStream">File contents.</param> /// <param name="uploadData">Upload parameters.</param> /// <param name="parentPath">Parent path.</param> /// <param name="server">Target server.</param> /// <param name="progressCallback">An optional callback method that is called after each chunk is uploaded to the server.</param> /// <returns>The uploaded file content returned at the end of the upload request.</returns> public static async Task <Content> UploadAsync(Stream binaryStream, UploadData uploadData, string parentPath, ServerContext server = null, Action <int> progressCallback = null) { var requestData = new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), ActionName = "Upload", Path = parentPath }; return(await UploadInternalAsync(binaryStream, uploadData, requestData, server, progressCallback)); }
/// <summary> /// Gets the raw response of an OData single content request from the server. /// </summary> /// <param name="path">Content path.</param> /// <param name="actionName">Action name.</param> /// <param name="method">HTTP method (SenseNet.Client.HttpMethods class has a few predefined methods).</param> /// <param name="body">Request body.</param> /// <param name="server">Target server.</param> /// <returns>Raw HTTP response.</returns> public static async Task <string> GetResponseStringAsync(string path, string actionName = null, HttpMethod method = null, string body = null, ServerContext server = null) { var requestData = new ODataRequest { SiteUrl = ServerContext.GetUrl(server), Path = path, ActionName = actionName }; return(await GetResponseStringAsync(requestData.GetUri(), server, method, body)); }
private static async Task <dynamic> PostContentInternalAsync(int contentId, object postData, HttpMethod method, ServerContext server = null) { var reqData = new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), ContentId = contentId }; var rs = await GetResponseStringAsync(reqData.GetUri(), server, method, JsonHelper.GetJsonPostModel(postData)); return(JsonHelper.Deserialize(rs).d); }
/// <summary> /// Initializes an instance of the ODataRequest class. /// </summary> public ODataRequest(ServerContext server) { // set default values Parameters = new ODataRequestParameterCollection(AddWellKnownItem, RemoveWellKnownItem); Metadata = MetadataFormat.None; SiteUrl = ServerContext.GetUrl(server); if (string.IsNullOrEmpty(SiteUrl)) { throw new InvalidOperationException("SiteUrl is missing. Please configure or provide a server context."); } }
//============================================================================= Constructors and overrides /// <summary> /// Initializes an instance of the ODataRequest class. /// </summary> public ODataRequest() { // set default values Parameters = new Dictionary <string, string>(); Metadata = MetadataFormat.None; SiteUrl = ServerContext.GetUrl(null); //InlineCount = InlineCount.None; //Sort = new SortInfo[0]; //Select = new List<string>(); //Expand = new List<string>(); //AutofiltersEnabled = FilterStatus.Disabled; }
/// <summary> /// Queries the server for content items using the provided request data. /// </summary> /// <param name="requestData">OData request parameters, for example select or expand.</param> /// <param name="server">Target server.</param> public static async Task <IEnumerable <Content> > GetCollectionAsync(ODataRequest requestData, ServerContext server = null) { requestData.SiteUrl = ServerContext.GetUrl(server); // just to make sure requestData.IsCollectionRequest = true; var rs = await GetResponseStringAsync(requestData.GetUri(), server); var items = JsonHelper.Deserialize(rs).d.results as JArray; return(items?.Select(c => Content.CreateFromResponse(c, server)) ?? new Content[0]); }
/// <summary> /// Checks whether a content exists on the server with the provided path. /// </summary> /// <param name="path">Content path.</param> /// <param name="server">Target server.</param> public static async Task <bool> ExistsAsync(string path, ServerContext server = null) { var requestData = new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), Path = path, Metadata = MetadataFormat.None, Select = new[] { "Id" } }; var content = await RESTCaller.GetContentAsync(requestData); return(content != null); }
public static async Task GetStreamResponseAsync(int contentId, string version, string propertyName, ServerContext server, Action <HttpResponseMessage> responseProcessor, CancellationToken cancellationToken) { if (server == null) { server = ClientContext.Current.Server; } var url = $"{ServerContext.GetUrl(server)}/binaryhandler.ashx?nodeid={contentId}&propertyname={propertyName ?? "Binary"}"; if (!string.IsNullOrEmpty(version)) { url += "&version=" + version; } await ProcessWebResponseAsync(url, HttpMethod.Get, server, responseProcessor, cancellationToken) .ConfigureAwait(false); }
/// <summary> /// Executes a count-only query in a subfolder on the server. /// </summary> /// <param name="path">Content path.</param> /// <param name="query">Content query text. If it is empty, the count of children will be returned.</param> /// <param name="server">Target server.</param> /// <returns>Count of result content.</returns> public static async Task <int> GetCountAsync(string path, string query, ServerContext server = null) { var request = new ODataRequest { SiteUrl = ServerContext.GetUrl(server), Path = path, IsCollectionRequest = true, CountOnly = true }; if (!string.IsNullOrEmpty(query)) { request.Parameters.Add("query", query); } return(await RESTCaller.GetCountAsync(request, server)); }