Beispiel #1
0
        private static List LoadCurrentList(Web web, ListDefinition listModel)
        {
            var context = web.Context;

            List currentList = null;

            var listUrl = UrlUtility.CombineUrl(web.ServerRelativeUrl, listModel.GetListUrl());

            Folder folder = null;

            var scope = new ExceptionHandlingScope(context);

            using (scope.StartScope())
            {
                using (scope.StartTry())
                {
                    folder = web.GetFolderByServerRelativeUrl(listUrl);
                    context.Load(folder);
                }

                using (scope.StartCatch())
                {

                }
            }

            context.ExecuteQueryWithTrace();

            if (!scope.HasException && folder != null && folder.ServerObjectIsNull != true)
            {
                folder = web.GetFolderByServerRelativeUrl(listUrl);
                context.Load(folder.Properties);
                context.ExecuteQueryWithTrace();

                var listId = new Guid(folder.Properties["vti_listname"].ToString());
                var list = web.Lists.GetById(listId);

                context.Load(list);
                context.ExecuteQueryWithTrace();

                currentList = list;
            }

            return currentList;
        }
Beispiel #2
0
        private static SPList GetListByUrl(SPWeb web, ListDefinition listModel)
        {
            SPList result;

            try
            {
                var targetListUrl = SPUrlUtility.CombineUrl(web.Url, listModel.GetListUrl());
                result = web.GetList(targetListUrl);
            }
            catch
            {
                result = null;
            }

            return result;
        }
        private static List LoadCurrentList(Web web, ListDefinition listModel)
        {
            var context = web.Context;

            List currentList = null;

            #pragma warning disable 618
            var listUrl = UrlUtility.CombineUrl(web.ServerRelativeUrl, listModel.GetListUrl());
            #pragma warning restore 618

            Folder folder;

            var scope = new ExceptionHandlingScope(context);

            using (scope.StartScope())
            {
                using (scope.StartTry())
                {
                    folder = web.GetFolderByServerRelativeUrl(listUrl);
                    context.Load(folder);
                }

                using (scope.StartCatch())
                {

                }
            }

            context.ExecuteQueryWithTrace();

            if (!scope.HasException && folder != null && folder.ServerObjectIsNull != true)
            {
            #if !NET35
                folder = web.GetFolderByServerRelativeUrl(listUrl);

                context.Load(folder.Properties);
                context.ExecuteQueryWithTrace();

                var listId = new Guid(folder.Properties["vti_listname"].ToString());
                var list = web.Lists.GetById(listId);

                context.Load(list);

                if (listModel.IndexedRootFolderPropertyKeys.Any())
                {
                    context.Load(list, l => l.RootFolder.Properties);
                }

                context.ExecuteQueryWithTrace();

                currentList = list;

            #endif

            #if NET35

                // SP2010 CSOM hack
                // http://impl.com/questions/4284722/sharepoint-2010-client-object-model-get-a-list-item-from-a-url

                var listQuery = from list in web.Lists
                                where list.RootFolder.ServerRelativeUrl == listUrl
                                select list;

                var queryResult = context.LoadQuery(listQuery);
                context.ExecuteQueryWithTrace();

                var resultList = queryResult.FirstOrDefault();

                currentList = resultList;
            #endif
            }

            return currentList;
        }
Beispiel #4
0
        private SPList GetOrCreateList(
            object modelHost,
            SPWeb web, ListDefinition listModel)
        {
            var result = GetListByUrl(web, listModel);

            if (result == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list");

                var listId = default(Guid);

                // "SPBug", there are two ways to create lists 
                // (1) by TemplateName (2) by TemplateType 
                if (listModel.TemplateType > 0)
                {
                    TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Creating list by TemplateType: [{0}]", listModel.TemplateType);

                    //listId = web.Lists.Add(listModel.Url, listModel.Description ?? string.Empty, (SPListTemplateType)listModel.TemplateType);
                    listId = web.Lists.Add(
                                    listModel.Title,
                                    listModel.Description ?? string.Empty,
                                    listModel.GetListUrl(),
                                    string.Empty,
                                    (int)listModel.TemplateType,
                                    string.Empty);
                }
                else if (!string.IsNullOrEmpty(listModel.TemplateName))
                {
                    TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Creating list by TemplateName: [{0}]", listModel.TemplateName);

                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Searching list template in web.ListTemplates");
                    var listTemplate = ResolveListTemplate(web, listModel);

                    listId = web.Lists.Add(
                                   listModel.Title,
                                   listModel.Description ?? string.Empty,
                                   listModel.GetListUrl(),
                                   listTemplate.FeatureId.ToString(),
                                   (int)listTemplate.Type,
                                   listTemplate.DocumentTemplate);
                }
                else
                {
                    throw new ArgumentException("TemplateType or TemplateName must be defined");
                }

                result = web.Lists[listId];

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = result,
                    ObjectType = typeof(SPList),
                    ObjectDefinition = listModel,
                    ModelHost = modelHost
                });
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list");

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = result,
                    ObjectType = typeof(SPList),
                    ObjectDefinition = listModel,
                    ModelHost = modelHost
                });
            }

            return result;
        }