예제 #1
0
        public JsonResult GetRootSiteUrl()
        {
            string rootSiteUrl = null;
            var    isSuccess   = false;

            try
            {
                var hostUrl = SpManager.GetHostParam(HttpContext.Request);

                using (var clientContext = GetClientContext(hostUrl))
                {
                    var site = clientContext.Site;
                    clientContext.Load(site);
                    clientContext.ExecuteQuery();

                    rootSiteUrl = site.Url;
                }

                isSuccess = true;
            }
            catch (Exception ex)
            {
                rootSiteUrl = ex.StackTrace;
            }

            return(Json(new
            {
                Success = isSuccess,
                Url = rootSiteUrl
            }, JsonRequestBehavior.AllowGet));
        }
예제 #2
0
        public ActionResult SharePointFile(string path)
        {
            try
            {
                var hostUrl = SpManager.GetHostParam(HttpContext.Request);

                using (var clientContext = GetClientContext(hostUrl))
                {
                    var file = clientContext.Web.GetFileByServerRelativeUrl(path);

                    var clientStream = file.OpenBinaryStream();

                    clientContext.Load(file);
                    clientContext.ExecuteQuery();

                    using (var stream = clientStream.Value)
                        using (var reader = new StreamReader(stream))
                        {
                            var contents = reader.ReadToEnd();

                            return(new ObjectResult <object>(GetJsonResponse(data: contents)));
                        }
                }
            }
            catch (Exception e)
            {
                return(Json(new { error = e.Message }, JsonRequestBehavior.AllowGet));
            }
        }